-
[C#] UnitTestC#/기초 2024. 3. 19. 08:04
NUnit vs. XUnit vs. MSTest
We now compare the C# unit testing frameworks from an attribute usage point of view along with a simple example, which demonstrates the code flow.
Attributes in test frameworks
Irrespective of the C# unit testing framework, attributes are used to describe class, methods, properties, etc. Attributes are meta-tags that provide additional information about the implementation under that particular tag.
To make the NUnit vs. XUnit vs. MSTest comparison clearer, we cover the important attributes in each of these frameworks. The NUnit vs. XUnit vs. MSTest attributes comparison will also help in porting the test implementation from one test framework to another.
DESCRIPTIONNUNITMSTESTXUNITMarks a test method/individual test [Test] [TestMethod] [Fact] Indicates that a class has a group of unit tests [TestFixture] [TestClass] N.A Contains the initialization code, which is triggered before every test case [SetUp] [TestInitialize] Constructor Contains the cleanup code, which is triggered after every test case [TearDown] [TestCleanup] IDisposable.Dispose Contains method that is triggered once before test cases start [OneTimeSetUp] [ClassInitialize] IClassFixture<T> Contains method that is triggered once before test cases end [OneTimeTearDown] [ClassCleanup] IClassFixture<T> Contains per-collection fixture setup and teardown N.A N.A ICollectionFixture<T> Ignores a test case [Ignore(“reason”)] [Ignore] [Fact(Skip=”reason”)] Categorize test cases or classes [Category()] [TestCategory(“)] [Trait(“Category”, “”) Identifies a method that needs to be called before executing any test in test class/test fixture [TestFixtureSetup] [ClassInitialize] N.A Identifies a method that needs to be called after executing any test in test class/test fixture [TestFixtureTearDown] [ClassCleanUp] N.A Identifies a method that needs to be called before the execution of any tests in Test Assembly N.A [AssemblyInitialize] N.A Identifies a method that needs to be called after execution of tests in Test Assembly N.A [AssemblyCleanUp] N.A
https://www.lambdatest.com/blog/nunit-vs-xunit-vs-mstest/
public 안하면 debug test 안됨.
TestMothod 에 객체 파라미터 여러개 전달.
[TestMethod] [DynamicData(nameof(AdditionData))] public void TestRegisterServiceToItems(AddressInfo addressInfo) { var beforeCount = managerServiceModel.ServiceItems.Count; managerServiceModel.RegisterServiceToItems(addressInfo); var afterCount = managerServiceModel.ServiceItems.Count; Assert.AreNotSame(beforeCount, afterCount); } public static IEnumerable<object[]> AdditionData { get { return new[] { new object[] { new AddressInfo { HostName = "TestHostName1", ServiceName = "TestService1", Ip = "192.168.0.1", Port = 0001, }, }, new object[] { new AddressInfo { HostName = "TestHostName1", ServiceName = "TestService1", Ip = "192.168.0.2", Port = 0002, }, }, new object[] { new AddressInfo { HostName = "TestHostName3", ServiceName = "TestService3", Ip = "192.168.0.3", Port = 0003, }, }, }; } }
728x90'C# > 기초' 카테고리의 다른 글
[C#] if (A is {b : c}) (0) 2024.03.27 [C#] Mutex (0) 2024.03.27 [C#] default(T); (0) 2024.03.06 [C#] NameSpace Brace ; (0) 2024.02.26 [C#] get=> , get; (0) 2024.02.14 댓글