C#/기초

[C#] UnitTest

딸기우유중독 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.

 

DESCRIPTION                                                                  NUNIT               MSTEST             XUNIT

 

Marks 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/

 

NUnit vs. XUnit vs. MSTest: Unit Testing Frameworks | LambdaTest

Explore the battle of NUnit, xUnit, and MSTest. A detailed comparison of unit testing frameworks in C#. Make the right choice!

www.lambdatest.com

 

https://dlsenfl.tistory.com/entry/C-%EC%A3%BC%EC%84%9D

 

[C#] // 주석

https://learn.microsoft.com/ko-kr/dotnet/csharp/language-reference/xmldoc/recommended-tags 권장 XML 문서 태그 - C# reference이 문서에서는 XML 문서의 권장 유형 태그와 멤버에 대한 구문 및 정의를 제공합니다.learn.microso

dlsenfl.tistory.com

 

 


test not  run ( skip)   

Nunit 쓰다가 MSTest 로 바꾸면 

test not  run ( skip) 함. 왜인지 이오 못 찾음.

 

 


 

build 실패

TargetFramework 바꾸면 빌드 실패. 

=> CleanBuild 하고 TargetFramework 바꾸고 Rebuild


 

 


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,
                    },
                },
            };
        }
    }

 

 


 

https://learn.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2022

 

Create Data-Driven Unit Tests - Visual Studio (Windows)

Learn how to use the Microsoft unit test framework for managed code to set up a unit test method to retrieve values from a data source.

learn.microsoft.com

 

https://stackoverflow.com/questions/51345370/unit-testing-sending-complex-object-as-a-input-parameter-to-my-test-method-usin

 

Unit testing: Sending complex object as a input parameter to my test method using MSTest

I'm trying to send a List<DataStatusItem> as a input parameter to my unit test method using DataRow attribute as below, [TestClass] public class UpdateProcessingTestController { private ...

stackoverflow.com

 

 

 

728x90