xUnitで使用できるAssertion

C#のテストツール「xUnit」について調べ中。

xUnitを使用したC#のテストプログラム作成法
xUnitを使用したC#のテストでSetUpやTearDown、Theory


使用できるAssertionについて調べてみます。

http://xunit.codeplex.com/wikipage?title=Comparisons&referringTitle=HowToUse
こちらの「Assertions」を参考にしました。



※思いのほか長くなったので、ページ内リンクを作っておきます。

等しいか(Equal)
等しくないか(NotEqual)
同じインスタンスでないか(NotSame)
同じインスタンスであるか(Same)
コレクションに含まれるか(Contains)
コレクションに含まれないか(DoesNotContain)
エラーが発生しないか(DoesNotThrow)
指定範囲に含まれるか(InRange)
指定クラスのインスタンスであるか(IsAssignableFrom)
空であるか(Empty)
Falseであるか(False)
指定のインスタンスであるか(IsType)
空でないか(NotEmpty)
指定のインスタンスではないか(IsNotType)
Nullでないか(NotNull)
nullであるか(Null)
trueであるか(True)
範囲に含まれないか(NotInRange)
指定のエラーが発生するか(Throws)





等しいか(Equal)



第一引数と第二引数が等しければ検証通過。
等しくなければエラーを報告します。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1() {
  5.         //成功
  6.         Assert.Equal(true, true);
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2() {
  11.         //エラー
  12.         Assert.Equal(true, false);
  13.     }
  14. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.Equal() Failure
Expected: True
Actual: False
Stack Trace:
     場所 Xunit.Assert.Equal[T](T expected, T actual, IEqualityComparer`1 comparer)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.315 seconds







等しくないか(NotEqual)



第一引数と第二引数が等しくなければ検証通過。
等しければエラーを報告します。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1() {
  5.         //成功
  6.         Assert.NotEqual(true, false);
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2() {
  11.         //エラー
  12.         Assert.NotEqual(true, true);
  13.     }
  14. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.NotEqual() Failure
Stack Trace:
     場所 Xunit.Assert.NotEqual[T](T expected, T actual)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.335 seconds







同じインスタンスでないか(NotSame)



第一引数と第二引数のインスタンスが等しくなければ検証通過。
等しければエラーを報告します。


  1. using Xunit;
  2. public class MyTests {
  3.     private TestData test_value = new TestData();
  4.     [Fact]
  5.     public void MyTest1() {
  6.         //成功
  7.         Assert.NotSame(test_value, new TestData());
  8.     }
  9.     
  10.     [Fact]
  11.     public void MyTest2() {
  12.         //エラー
  13.         Assert.NotSame(test_value, test_value);
  14.     }
  15. }
  16. public class TestData {
  17. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.NotSame() Failure
Stack Trace:
     場所 Xunit.Assert.NotSame(Object expected, Object actual)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.318 seconds







同じインスタンスであるか(Same)



第一引数と第二引数のインスタンスが等しければ検証通過。
等しくなければエラーを報告します。


  1. using Xunit;
  2. public class MyTests {
  3.     private TestData test_value = new TestData();
  4.     [Fact]
  5.     public void MyTest1() {
  6.         //成功
  7.         Assert.Same(test_value, test_value);
  8.     }
  9.     
  10.     [Fact]
  11.     public void MyTest2() {
  12.         //エラー
  13.         Assert.Same(test_value, new TestData());
  14.     }
  15. }
  16. public class TestData {
  17. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.Same() Failure
Expected: TestData
Actual: TestData
Stack Trace:
     場所 Xunit.Assert.Same(Object expected, Object actual)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.312 seconds








コレクションに含まれるか(Contains)



第一引数のコレクションに第二引数の値が含まれていれば検証通過。
含まれていなければエラーを報告します。


  1. using System.Collections.Generic;
  2. using Xunit;
  3. public class MyTests {
  4.     private List<int> test_value;
  5.     
  6.     public MyTests() {
  7.         test_value = new List<int>();
  8.         test_value.Add(10);
  9.         test_value.Add(20);
  10.         test_value.Add(30);
  11.     }
  12.     [Fact]
  13.     public void MyTest1() {
  14.         //成功
  15.         Assert.Contains(20, test_value);
  16.     }
  17.     
  18.     [Fact]
  19.     public void MyTest2() {
  20.         //エラー
  21.         Assert.Contains(40, test_value);
  22.     }
  23. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.Contains() failure: Not found: 40
Stack Trace:
     場所 Xunit.Assert.Contains[T](T expected, IEnumerable`1 collection, IEqualityComparer`1 comparer)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.309 seconds







コレクションに含まれないか(DoesNotContain)



第一引数のコレクションに第二引数の値が含まれていなけれは検証通過。
含まれていればエラーを報告します。


  1. using System.Collections.Generic;
  2. using Xunit;
  3. public class MyTests {
  4.     private List<int> test_value;
  5.     
  6.     public MyTests() {
  7.         test_value = new List<int>();
  8.         test_value.Add(10);
  9.         test_value.Add(20);
  10.         test_value.Add(30);
  11.     }
  12.     [Fact]
  13.     public void MyTest1() {
  14.         //成功
  15.         Assert.DoesNotContain(40, test_value);
  16.     }
  17.     
  18.     [Fact]
  19.     public void MyTest2() {
  20.         //エラー
  21.         Assert.DoesNotContain(20, test_value);
  22.     }
  23. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.DoesNotContain() failure: Found: 20
Stack Trace:
     場所 Xunit.Assert.DoesNotContain[T](T expected, IEnumerable`1 collection, IEqualityComparer`1 comparer)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.307 seconds







エラーが発生しないか(DoesNotThrow)



エラーが発生しなければ検証通過。
エラーが発生したらテストを通過しません。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1() {
  5.         //成功
  6.         Assert.DoesNotThrow(() => calc(10, 5) );
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2() {
  11.         //エラー
  12.         Assert.DoesNotThrow(() => calc(10, 0));
  13.     }
  14.     
  15.     private int calc(int a, int b) {
  16.         return a / b;
  17.     }
  18. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.DoesNotThrow() failure
Expected: (No exception)
Actual: System.DivideByZeroException: 0 で除算しようとしました。
Stack Trace:
     場所 MyTests.<MyTest2>b__1()
     場所 Xunit.Record.Exception(ThrowsDelegate code)

2 total, 1 failed, 0 skipped, took 0.316 seconds







指定範囲に含まれるか(InRange)



第一引数が、第二引数と第三引数の範囲に含まれていれば検証通過。
含まれていなければエラーとなります。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1() {
  5.         //成功
  6.         Assert.InRange(5, 1, 10);
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2() {
  11.         //エラー
  12.         Assert.InRange(15, 1, 10);
  13.     }
  14. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.InRange() Failure
Range: (1 - 10)
Actual: 15
Stack Trace:
     場所 Xunit.Assert.InRange[T](T actual, T low, T high, IComparer`1 comparer)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.318 seconds








指定クラスのインスタンスであるか(IsAssignableFrom)



よくわかっていないのですが、指定のクラスであるかのチェックだと思います。
書き方が2通りあって、


Assert.IsAssignableFrom(typeof(string), "日本語");



という記載方法と、


Assert.IsAssignableFrom<string>("日本語");



という記載方法があります。



  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1_1() {
  5.         //成功
  6.         Assert.IsAssignableFrom(typeof(string), "日本語");
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest1_2() {
  11.         //成功
  12.         Assert.IsAssignableFrom<string>("日本語");
  13.     }
  14.     
  15.     [Fact]
  16.     public void MyTest2_1() {
  17.         //エラー
  18.         Assert.IsAssignableFrom(typeof(string), 10);
  19.     }
  20.     
  21.     [Fact]
  22.     public void MyTest2_2() {
  23.         //エラー
  24.         Assert.IsAssignableFrom<string>(10);
  25.     }
  26. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2_2 [FAIL]
Assert.IsAssignableFrom() Failure
Expected: System.String
Actual: System.Int32
Stack Trace:
     場所 Xunit.Assert.IsAssignableFrom(Type expectedType, Object object)
     場所 Xunit.Assert.IsAssignableFrom[T](Object object)
     場所 MyTests.MyTest2_2()

MyTests.MyTest2_1 [FAIL]
Assert.IsAssignableFrom() Failure
Expected: System.String
Actual: System.Int32
Stack Trace:
     場所 Xunit.Assert.IsAssignableFrom(Type expectedType, Object object)
     場所 MyTests.MyTest2_1()

4 total, 2 failed, 0 skipped, took 0.311 seconds









空であるか(Empty)



文字列やリストが空白であるかを検証します。
nullのチェックではありません。


  1. using System.Collections.Generic;
  2. using Xunit;
  3. public class MyTests {
  4.     [Fact]
  5.     public void MyTest1_1() {
  6.         //成功
  7.         Assert.Empty("");
  8.     }
  9.     
  10.     [Fact]
  11.     public void MyTest1_2() {
  12.         //成功
  13.         Assert.Empty(new List<string>());
  14.     }
  15.     
  16.     [Fact]
  17.     public void MyTest2_1() {
  18.         //エラー
  19.         Assert.Empty("日本語");
  20.     }
  21.     
  22.     [Fact]
  23.     public void MyTest2_2() {
  24.         //エラー
  25.         Assert.Empty(null);
  26.     }
  27. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2_2 [FAIL]
System.ArgumentNullException : 値を Null にすることはできません。
パラメータ名: collection
Stack Trace:
     場所 Xunit.Assert.Empty(IEnumerable collection)
     場所 MyTests.MyTest2_2()

MyTests.MyTest2_1 [FAIL]
Assert.Empty() failure
Stack Trace:
     場所 Xunit.Assert.Empty(IEnumerable collection)
     場所 MyTests.MyTest2_1()

4 total, 2 failed, 0 skipped, took 0.316 seconds








Falseであるか(False)



これはそのまま、Falseだったら検証通過。
Trueだったらエラーとなります。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1() {
  5.         //成功
  6.         Assert.False(1 == 2);
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2() {
  11.         //エラー
  12.         Assert.False(1 == 1);
  13.     }
  14.     
  15. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.False() Failure
Stack Trace:
     場所 Xunit.Assert.False(Boolean condition)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.291 seconds









指定のインスタンスであるか(IsType)



これも2通り記載方法があります。


Assert.IsType(typeof(string), "文字列")



という記載方法と


Assert.IsType<string>("文字列")



という記載です。



  1. using Xunit;
  2. public class MyTests {
  3.     
  4.     private TestB test = new TestB();
  5.     [Fact]
  6.     public void MyTest1_1() {
  7.         //成功
  8.         Assert.IsType(typeof(TestB), test);
  9.     }
  10.     
  11.     [Fact]
  12.     public void MyTest1_2() {
  13.         //成功
  14.         Assert.IsType<TestB>(test);
  15.     }
  16.     
  17.     [Fact]
  18.     public void MyTest2_1() {
  19.         //エラー
  20.         Assert.IsType(typeof(TestA), test);
  21.     }
  22.     
  23.     [Fact]
  24.     public void MyTest2_2() {
  25.         //エラー
  26.         Assert.IsType<TestA>(test);
  27.     }
  28.     
  29. }
  30. public class TestA {
  31. }
  32. public class TestB : TestA {
  33. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2_2 [FAIL]
Assert.IsType() Failure
Expected: TestA
Actual: TestB
Stack Trace:
     場所 Xunit.Assert.IsType(Type expectedType, Object object)
     場所 Xunit.Assert.IsType[T](Object object)
     場所 MyTests.MyTest2_2()

MyTests.MyTest2_1 [FAIL]
Assert.IsType() Failure
Expected: TestA
Actual: TestB
Stack Trace:
     場所 Xunit.Assert.IsType(Type expectedType, Object object)
     場所 MyTests.MyTest2_1()

4 total, 2 failed, 0 skipped, took 0.327 seconds





ちなみに、上記のテストをIsTypeからIsAssignableFromに変更して実行してみます。


  1. using Xunit;
  2. public class MyTests {
  3.     
  4.     private TestB test = new TestB();
  5.     [Fact]
  6.     public void MyTest1_1() {
  7.         //成功
  8.         Assert.IsAssignableFrom(typeof(TestB), test);
  9.     }
  10.     
  11.     [Fact]
  12.     public void MyTest1_2() {
  13.         //成功
  14.         Assert.IsAssignableFrom<TestB>(test);
  15.     }
  16.     
  17.     [Fact]
  18.     public void MyTest2_1() {
  19.         //エラー
  20.         Assert.IsAssignableFrom(typeof(TestA), test);
  21.     }
  22.     
  23.     [Fact]
  24.     public void MyTest2_2() {
  25.         //エラー
  26.         Assert.IsAssignableFrom<TestA>(test);
  27.     }
  28.     
  29. }
  30. public class TestA {
  31. }
  32. public class TestB : TestA {
  33. }




テストは全て通過します。


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

4 total, 0 failed, 0 skipped, took 0.306 seconds








空でないか(NotEmpty)



Emptyの逆です。
空でなければ検証通過。
要素が空ならエラーになります。


  1. using System.Collections.Generic;
  2. using Xunit;
  3. public class MyTests {
  4.     [Fact]
  5.     public void MyTest1_1() {
  6.         //成功
  7.         Assert.NotEmpty("日本語");
  8.     }
  9.     
  10.     [Fact]
  11.     public void MyTest1_2() {
  12.         //成功
  13.         List<int> list = new List<int>();
  14.         list.Add(1);
  15.         Assert.NotEmpty(list);
  16.     }
  17.     
  18.     [Fact]
  19.     public void MyTest2_1() {
  20.         //エラー
  21.         Assert.NotEmpty("");
  22.     }
  23.     
  24.     [Fact]
  25.     public void MyTest2_2() {
  26.         //エラー
  27.         Assert.NotEmpty(new List<int>());
  28.     }
  29.     
  30. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2_2 [FAIL]
Assert.NotEmpty() failure
Stack Trace:
     場所 Xunit.Assert.NotEmpty(IEnumerable collection)
     場所 MyTests.MyTest2_2()

MyTests.MyTest2_1 [FAIL]
Assert.NotEmpty() failure
Stack Trace:
     場所 Xunit.Assert.NotEmpty(IEnumerable collection)
     場所 MyTests.MyTest2_1()

4 total, 2 failed, 0 skipped, took 0.303 seconds








指定のインスタンスではないか(IsNotType)



IsTypeの逆です。
指定のインスタンスでなければ検証通過。
指定のインスタンスだったらエラーになります。


  1. using System.Collections.Generic;
  2. using Xunit;
  3. public class MyTests {
  4.     private ClassB test = new ClassB();
  5.     [Fact]
  6.     public void MyTest1_1() {
  7.         //成功
  8.         Assert.IsNotType(typeof(ClassA), test);
  9.     }
  10.     
  11.     [Fact]
  12.     public void MyTest1_2() {
  13.         //成功
  14.         Assert.IsNotType<ClassA>(test);
  15.     }
  16.     
  17.     [Fact]
  18.     public void MyTest2_1() {
  19.         //エラー
  20.         Assert.IsNotType(typeof(ClassB), test);
  21.     }
  22.     
  23.     [Fact]
  24.     public void MyTest2_2() {
  25.         //エラー
  26.         Assert.IsNotType<ClassB>(test);
  27.     }
  28.     
  29. }
  30. public class ClassA {
  31. }
  32. public class ClassB : ClassA {
  33. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2_1 [FAIL]
Assert.IsNotType() Failure
Expected: ClassB
Actual: ClassB
Stack Trace:
     場所 Xunit.Assert.IsNotType(Type expectedType, Object object)
     場所 MyTests.MyTest2_1()

MyTests.MyTest2_2 [FAIL]
Assert.IsNotType() Failure
Expected: ClassB
Actual: ClassB
Stack Trace:
     場所 Xunit.Assert.IsNotType(Type expectedType, Object object)
     場所 Xunit.Assert.IsNotType[T](Object object)
     場所 MyTests.MyTest2_2()

4 total, 2 failed, 0 skipped, took 0.314 seconds








Nullでないか(NotNull)



nullでなければ検証通過。
nullならエラーになります。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1_1() {
  5.         //成功
  6.         Assert.NotNull("日本語");
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2_1() {
  11.         //エラー
  12.         Assert.NotNull(null);
  13.     }
  14.     
  15. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2_1 [FAIL]
Assert.NotNull() Failure
Stack Trace:
     場所 Xunit.Assert.NotNull(Object object)
     場所 MyTests.MyTest2_1()

2 total, 1 failed, 0 skipped, took 0.310 seconds








nullであるか(Null)



NotNullの逆です。
nullなら検証通過。
nullでなければエラーとなります。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1() {
  5.         //成功
  6.         Assert.Null(null);
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2() {
  11.         //エラー
  12.         Assert.Null("日本語");
  13.     }
  14.     
  15. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.Null() Failure
Expected: (null)
Actual: 日本語
Stack Trace:
     場所 Xunit.Assert.Null(Object object)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.311 seconds








trueであるか(True)



Falseの逆。
trueなら検証通過。
falseならエラーを報告します。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1() {
  5.         //成功
  6.         Assert.True(1 == 1);
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2() {
  11.         //エラー
  12.         Assert.True(1 == 2);
  13.     }
  14.     
  15. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.True() Failure
Stack Trace:
     場所 Xunit.Assert.True(Boolean condition)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.305 seconds







範囲に含まれないか(NotInRange)



InRangeの逆。
第一引数が、第二引数と第三引数の範囲に含まれなければ検証通過。
含まれていたらエラーになります。

こんな感じで、文字の範囲も指定可能です。


  1. using Xunit;
  2. public class MyTests {
  3.     [Fact]
  4.     public void MyTest1() {
  5.         //成功
  6.         Assert.NotInRange("a", "x", "z");
  7.     }
  8.     
  9.     [Fact]
  10.     public void MyTest2() {
  11.         //エラー
  12.         Assert.NotInRange("y", "x", "z");
  13.     }
  14.     
  15. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2 [FAIL]
Assert.NotInRange() Failure
Range: (x - z)
Actual: y
Stack Trace:
     場所 Xunit.Assert.NotInRange[T](T actual, T low, T high, IComparer`1 comparer)
     場所 Xunit.Assert.NotInRange[T](T actual, T low, T high)
     場所 MyTests.MyTest2()

2 total, 1 failed, 0 skipped, took 0.311 seconds







指定のエラーが発生するか(Throws)



指定したエラーが発生したら検証通過。
発生しなければエラーになります。

これも書き方が2通りあります。


Assert.Throws(typeof(Exception), hoge)
Assert.Throws<Exception>(hoge)




また、エラーの種類は厳密に書く必要があるみたいです。


  1. using System;
  2. using Xunit;
  3. public class MyTests {
  4.     [Fact]
  5.     public void MyTest1_1() {
  6.         //成功
  7.         Assert.Throws(typeof(DivideByZeroException), () => calc(0));
  8.     }
  9.     
  10.     [Fact]
  11.     public void MyTest1_2() {
  12.         //成功
  13.         Assert.Throws<DivideByZeroException>(() => calc(0));
  14.     }
  15.     
  16.     [Fact]
  17.     public void MyTest2_1() {
  18.         //エラー
  19.         Assert.Throws(typeof(DivideByZeroException), () => calc(10));
  20.     }
  21.     
  22.     [Fact]
  23.     public void MyTest2_2() {
  24.         //エラー
  25.         Assert.Throws<DivideByZeroException>(() => calc(10));
  26.     }
  27.     
  28.     private int calc(int val) {
  29.         return 10 / val;
  30.     }
  31. }




テストの実行結果


xUnit.net console test runner (64-bit .NET 2.0.50727.5420)
Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:     Version 1.9.1.1600
Test assembly: c:¥share¥csharp¥MyTests.dll

MyTests.MyTest2_2 [FAIL]
Assert.Throws() Failure
Expected: System.DivideByZeroException
Actual: (No exception was thrown)
Stack Trace:
     場所 Xunit.Assert.Throws(Type exceptionType, ThrowsDelegateWithReturn testCode)
     場所 Xunit.Assert.Throws[T](ThrowsDelegateWithReturn testCode)
     場所 MyTests.MyTest2_2()

MyTests.MyTest2_1 [FAIL]
Assert.Throws() Failure
Expected: System.DivideByZeroException
Actual: (No exception was thrown)
Stack Trace:
     場所 Xunit.Assert.Throws(Type exceptionType, ThrowsDelegateWithReturn testCode)
     場所 MyTests.MyTest2_1()

4 total, 2 failed, 0 skipped, took 0.322 seconds








【参考URL】

http://xunit.codeplex.com/wikipage?title=Comparisons&referringTitle=HowToUse

http://xunit.codeplex.com/SourceControl/BrowseLatest


関連記事

コメント

プロフィール

Author:symfo
blog形式だと探しにくいので、まとめサイト作成中です。
https://symfo.web.fc2.com/

PR

検索フォーム

月別アーカイブ