This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

xUnit.net の使い方(Assert 編)

Last updated at Posted at 2016-06-29

単純比較

  • 期待される答え (expected) と、実際の実行結果 (actual) を与えて比較する。
  • 2つが同じなら成功(メソッド名の通りになっていれば成功)
Assert.Equal("期待される答え", "実際の実行結果");

Assert.Equal(2, MyMath.Add(1, 1));
メソッド 説明
Assert.Equal<T>(T expected, T actual)
Assert.Equal(decimal expected, decimal actual, int precision) 同じなら成功。10進数で小数点以下 precision 桁になるように丸め込んで比較する。
Assert.Equal(double expected, double actual, int precision) 同じなら成功。10進数で小数点以下 precision 桁になるように丸め込んで比較する。
Assert.NotEqual<T>(T expected, T actual)
Assert.NotEqual(decimal expected, decimal actual, int precision) 同じなら成功。10進数で小数点以下 precision 桁になるように丸め込んで比較する。
Assert.NotEqual(double expected, double actual, int precision) 同じなら成功。10進数で小数点以下 precision 桁になるように丸め込んで比較する。
Assert.Equal<T>(T expected, T actual, IEqualityComparer<T> comparer)
Assert.NotEqual<T>(T expected, T actual, IEqualityComparer<T> comparer)

Boolean 系

True

メソッド 説明
Assert.True(bool condition) conditiontrue なら成功
Assert.True(bool? condition) conditiontrue なら成功
Assert.True(bool condition, string userMessage) conditiontrue なら成功。userMessage はメッセージ
Assert.True(bool? condition, string userMessage) conditiontrue なら成功。userMessage はメッセージ

False

メソッド 説明
Assert.False(bool condition) conditionfalse なら成功
Assert.False(bool? condition) conditionfalse なら成功
Assert.False(bool condition, string userMessage) conditionfalse なら成功。userMessage はメッセージ
Assert.False(bool? condition, string userMessage) conditionfalse なら成功。userMessage はメッセージ

文字列系

比較系

メソッド 説明
Assert.Equal(string expected, string actual) actualexpected と同じなら成功
Assert.Equal(string expected, string actual, bool ignoreCase = false, bool ignoreLineEndingDifferences = false, bool ignoreWhiteSpaceDifferences = false) actualexpected と同じなら成功
ignoreCase : true なら大文字と区別しない
ignoreLineEndingDifferences : true なら改行コードの違いを区別しない
ignoreWhiteSpaceDifferences : true ならホワイトスペースの違いを区別しない(全角スペースはダメ?)

包含系

メソッド 説明
Assert.EndsWith(string expectedEndString, string actualString)
Assert.EndsWith(string expectedEndString, string actualString, StringComparison comparisonType)
Assert.StartsWith(string expectedStartString, string actualString)
Assert.StartsWith(string expectedStartString, string actualString, StringComparison comparisonType)
Assert.Contains(string expectedSubstring, string actualString)
Assert.Contains(string expectedSubstring, string actualString, StringComparison comparisonType)
Assert.DoesNotContain(string expectedSubstring, string actualString)
Assert.DoesNotContain(string expectedSubstring, string actualString, StringComparison comparisonType)

正規表現系

メソッド 説明
Assert.DoesNotMatch(string expectedRegexPattern, string actualString)
Assert.DoesNotMatch(Regex expectedRegex, string actualString)
Assert.Matches(Regex expectedRegex, string actualString)
Assert.Matches(string expectedRegexPattern, string actualString)

Null

メソッド 説明
Assert.Null(object @object) @object の参照が null なら成功
Assert.NotNull(object @object) @objectnull なら成功

public static T IsAssignableFrom(object @object);
public static void IsAssignableFrom(Type expectedType, object @object);
public static void IsNotType(object @object);
public static void IsNotType(Type expectedType, object @object);
public static T IsType(object @object);
public static void IsType(Type expectedType, object @object);

例外

public static T Throws(Action testCode) where T : Exception;
public static T Throws(Func testCode) where T : Exception;
public static T Throws(string paramName, Action testCode) where T : ArgumentException;
public static T Throws(string paramName, Func testCode) where T : ArgumentException;
public static Exception Throws(Type exceptionType, Action testCode);
public static Exception Throws(Type exceptionType, Func testCode);
public static T ThrowsAny(Action testCode) where T : Exception;
public static T ThrowsAny(Func testCode) where T : Exception;
public static Task ThrowsAnyAsync(Func testCode) where T : Exception;
public static Task ThrowsAsync(Func testCode) where T : Exception;
public static Task ThrowsAsync(string paramName, Func testCode) where T : ArgumentException;
public static Task ThrowsAsync(Type exceptionType, Func testCode);

public static void All(IEnumerable collection, Action action);
public static void Collection(IEnumerable collection, params Action[] elementInspectors);
public static void Contains(IEnumerable collection, Predicate filter);
public static void Contains(T expected, IEnumerable collection);
public static void Contains(T expected, IEnumerable collection, IEqualityComparer comparer);
public static void DoesNotContain(IEnumerable collection, Predicate filter);
public static void DoesNotContain(T expected, IEnumerable collection);
public static void DoesNotContain(T expected, IEnumerable collection, IEqualityComparer comparer);
public static void Empty(IEnumerable collection);
public static void Equal(IEnumerable expected, IEnumerable actual);
public static void Equal(IEnumerable expected, IEnumerable actual, IEqualityComparer comparer);
public static void InRange(T actual, T low, T high) where T : IComparable;
public static void InRange(T actual, T low, T high, IComparer comparer);
public static void NotEmpty(IEnumerable collection);
public static void NotEqual(IEnumerable expected, IEnumerable actual);
public static void NotEqual(IEnumerable expected, IEnumerable actual, IEqualityComparer comparer);
public static void NotInRange(T actual, T low, T high) where T : IComparable;
public static void NotInRange(T actual, T low, T high, IComparer comparer);
public static void NotSame(object expected, object actual);
public static void NotStrictEqual(T expected, T actual);
public static void ProperSubset(ISet expectedSuperset, ISet actual);
public static void ProperSuperset(ISet expectedSubset, ISet actual);
public static void PropertyChanged(INotifyPropertyChanged @object, string propertyName, Action testCode);
protected static Exception RecordException(Func testCode);
protected static Task RecordExceptionAsync(Func testCode);
public static void Same(object expected, object actual);
public static T Single(IEnumerable collection);
public static object Single(IEnumerable collection);
public static T Single(IEnumerable collection, Predicate predicate);
public static void Single(IEnumerable collection, object expected);
public static void StrictEqual(T expected, T actual);
public static void Subset(ISet expectedSuperset, ISet actual);
public static void Superset(ISet expectedSubset, ISet actual);

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up