주 메뉴 열기

wwiki β

바뀜

XUnit.net

5,732 바이트 추가됨, 2021년 10월 28일 (목) 07:04
편집 요약 없음
=== Comparing xUnit.net to other frameworks ===
https://xunit.net/docs/comparisons
 
==== Attributes ====
{| class="wikitable"
|NUnit 3.x
|[TestInitialize]
|Constructor
|We believe that use of [SetUp] is generally bad. However, you can implement a parameterless constructor as a direct replacement. See [[XUnit.net#Note%202|Note 2]]
|-
|[TearDown]
|[TestCleanup]
|IDisposable.Dispose
|We believe that use of [TearDown] is generally bad. However, you can implement IDisposable.Dispose as a direct replacement. See [[XUnit.net#Note%202|Note 2]]
|-
|[OneTimeSetUp]
|[ClassInitialize]
|IClassFixture<T>
|To get per-class fixture setup, implement IClassFixture<T> on your test class. See [[XUnit.net#Note%203|Note 3]]
|-
|[OneTimeTearDown]
|[ClassCleanup]
|IClassFixture<T>
|To get per-class fixture teardown, implement IClassFixture<T> on your test class. See [[XUnit.net#Note%203|Note 3]]
|-
|''n/a''
|''n/a''
|ICollectionFixture<T>
|To get per-collection fixture setup and teardown, implement ICollectionFixture<T> on your test collection. See [[XUnit.net#Note%203|Note 3]]
|-
|[Ignore("reason")]
[XxxData]
|Theory (data-driven test). See [[XUnit.net#Note%204|Note 4]]
|}
===== Attribute Notes =====
====== Note 1 ======
기대되는 예외지정<syntaxhighlight lang="csharp">
#nullable enable
</syntaxhighlight>
====== Note 2 ====== 각 테스트를 실행하기 전에 실행할 코드를 지정할 때 사용한다.<syntaxhighlight lang="csharp">// Use TestInitialize to run code before running each test[TestInitialize()]public void MyTestInitialize(){ // To generate code for this test, select "Generate Code for Coded // UI Test" from the shortcut menu and select one of the menu items. // For more information on generated code, see // http://go.microsoft.com/fwlink/?LinkId=179463  // You could move this line from the CodedUITestMethod1() method this.UIMap.LaunchCalculator();} // Use TestCleanup to run code after each test has run[TestCleanup()]public void MyTestCleanup(){ // To generate code for this test, select "Generate Code for Coded // UI Test" from the shortcut menu and select one of the menu items. // For more information on generated code, see // http://go.microsoft.com/fwlink/?LinkId=179463  // You could move this line from the CodedUITestMethod1() method this.UIMap.CloseCalculator();}</syntaxhighlight> ====== Note 3 ====== 클래스의 첫 번째 테스트를 실행하기 전에 실행할 코드를 지정한다.<syntaxhighlight lang="csharp">using System; namespace SampleClassLib{ public class DivideClass { public static int DivideMethod(int denominator) { return (2 / denominator); } }}</syntaxhighlight>테스트 코드<syntaxhighlight lang="csharp">using Microsoft.VisualStudio.TestTools.UnitTesting;using SampleClassLib;using System;using System.Windows.Forms; namespace TestNamespace{ [TestClass()] public sealed class DivideClassTest { [AssemblyInitialize()] public static void AssemblyInit(TestContext context) { MessageBox.Show("AssemblyInit " + context.TestName); }  [ClassInitialize()] public static void ClassInit(TestContext context) { MessageBox.Show("ClassInit " + context.TestName); }  [TestInitialize()] public void Initialize() { MessageBox.Show("TestMethodInit"); }  [TestCleanup()] public void Cleanup() { MessageBox.Show("TestMethodCleanup"); }  [ClassCleanup()] public static void ClassCleanup() { MessageBox.Show("ClassCleanup"); }  [AssemblyCleanup()] public static void AssemblyCleanup() { MessageBox.Show("AssemblyCleanup"); }  [TestMethod()] [ExpectedException(typeof(System.DivideByZeroException))] public void DivideMethodTest() { DivideClass.DivideMethod(0); } }}</syntaxhighlight> ====== Note 4 ====== 데이터 기반 단위 테스트<syntaxhighlight lang="csharp">public int AddIntegers(int first, int second){ int sum = first; for( int i = 0; i < second; i++) { sum += 1; } return sum;}<br /syntaxhighlight>테스트 클래스에 TestContext 추가<syntaxhighlight lang="csharp">private TestContext testContextInstance;public TestContext TestContext{ get { return testContextInstance; } set { testContextInstance = value; }}</syntaxhighlight>테스트 메서드 작성<syntaxhighlight lang="csharp">[DataSource(@"Provider=Microsoft.SqlServerCe.Client.4.0; Data Source=C:\Data\MathsData.sdf;", "Numbers")][TestMethod()]public void AddIntegers_FromDataSourceTest(){ var target = new Maths();  // Access the data int x = Convert.ToInt32(TestContext.DataRow["FirstNumber"]); int y = Convert.ToInt32(TestContext.DataRow["SecondNumber"]); int expected = Convert.ToInt32(TestContext.DataRow["Sum"]); int actual = target.IntegerMethod(x, y); Assert.AreEqual(expected, actual, "x:<{0}> y:<{1}>", new object[] {x, y});}</syntaxhighlight> ==== Assertions ===={| class="wikitable"|'''NUnit 3.x (Constraint)'''|'''MSTest 15.x'''|'''xUnit.net 2.x'''|'''Comments'''|-|Is.EqualTo|AreEqual|Equal|MSTest and xUnit.net support generic versions of this method|-|Is.Not.EqualTo|AreNotEqual|NotEqual|MSTest and xUnit.net support generic versions of this method|-|Is.Not.SameAs|AreNotSame|NotSame||-|Is.SameAs|AreSame|Same||-|Does.Contain|Contains|Contains||-|Does.Not.Contain|DoesNotContain|DoesNotContain||-|Throws.Nothing|''n/a''|''n/a''|Ensures that the code does not throw any exceptions. See Note 5|-|''n/a''|Fail|''n/a''|xUnit.net alternative: Assert.True(false, "message")|-|Is.GreaterThan|''n/a''|''n/a''|xUnit.net alternative: Assert.True(x > y)|-|Is.InRange|''n/a''|InRange|Ensures that a value is in a given inclusive range|-|Is.AssignableFrom|''n/a''|IsAssignableFrom||-|Is.Empty|''n/a''|Empty||-|Is.False|IsFalse|False||-|Is.InstanceOf<T>|IsInstanceOfType|IsType<T>||-|Is.NaN|''n/a''|''n/a''|xUnit.net alternative: Assert.True(double.IsNaN(x))|-|Is.Not.AssignableFrom<T>|''n/a''|''n/a''|xUnit.net alternative: Assert.False(obj is Type)|-|Is.Not.Empty|''n/a''|NotEmpty||-|Is.Not.InstanceOf<T>|IsNotInstanceOfType|IsNotType<T>||-|Is.Not.Null|IsNotNull|NotNull||-|Is.Null|IsNull|Null||-|Is.True|IsTrue|True||-|Is.LessThan|''n/a''|''n/a''|xUnit.net alternative: Assert.True(x < y)|-|Is.Not.InRange|''n/a''|NotInRange|Ensures that a value is not in a given inclusive range|-|Throws.TypeOf<T>|''n/a''|Throws<T>|Ensures that the code throws an exact exception|}
{{DEFAULTSORT:xUnit.net}}
[[분류:.net]]
편집
2,431