Class Assert
The Assert class contains a collection of static methods that implement the most common assertions used in NUnit.
public abstract class Assert : AssertBase
- Inheritance
-
Assert
- Derived
- Inherited Members
Constructors
Assert()
protected Assert()
Methods
ByVal(object?, IResolveConstraint, string, string, string)
Apply a constraint to an actual value. Returns without throwing an exception when inside a multiple assert block. Used as a synonym for That in rare cases where a private setter causes a Visual Basic compilation error.
public static void ByVal(object? actual, IResolveConstraint expression, string message = "", string actualExpression = "", string constraintExpression = "")
Parameters
actual
objectThe actual value to test
expression
IResolveConstraintA Constraint expression to be applied
message
stringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Remarks
This method is provided for use by VB developers needing to test the value of properties with private setters.
Catch(TestDelegate)
Verifies that a delegate throws an exception when called and returns it. The returned exception may be null when inside a multiple assert block.
public static Exception? Catch(TestDelegate code)
Parameters
code
TestDelegateA TestDelegate
Returns
Catch(TestDelegate, string, params object?[]?)
Verifies that a delegate throws an exception when called and returns it. The returned exception may be null when inside a multiple assert block.
public static Exception? Catch(TestDelegate code, string message, params object?[]? args)
Parameters
code
TestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
Catch(Type, TestDelegate)
Verifies that a delegate throws an exception of a certain Type or one derived from it when called and returns it. The returned exception may be null when inside a multiple assert block.
public static Exception? Catch(Type expectedExceptionType, TestDelegate code)
Parameters
expectedExceptionType
TypeThe expected Exception Type
code
TestDelegateA TestDelegate
Returns
Catch(Type, TestDelegate, string, params object?[]?)
Verifies that a delegate throws an exception of a certain Type or one derived from it when called and returns it. The returned exception may be null when inside a multiple assert block.
public static Exception? Catch(Type expectedExceptionType, TestDelegate code, string message, params object?[]? args)
Parameters
expectedExceptionType
TypeThe expected Exception Type
code
TestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
CatchAsync(AsyncTestDelegate)
Verifies that an async delegate throws an exception when called and returns it. The returned exception may be null when inside a multiple assert block.
public static Exception? CatchAsync(AsyncTestDelegate code)
Parameters
code
AsyncTestDelegateA TestDelegate
Returns
CatchAsync(AsyncTestDelegate, string, params object?[]?)
Verifies that an async delegate throws an exception when called and returns it. The returned exception may be null when inside a multiple assert block.
public static Exception? CatchAsync(AsyncTestDelegate code, string message, params object?[]? args)
Parameters
code
AsyncTestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
CatchAsync(Type, AsyncTestDelegate)
Verifies that an async delegate throws an exception of a certain Type or one derived from it when called and returns it. The returned exception may be null when inside a multiple assert block.
public static Exception? CatchAsync(Type expectedExceptionType, AsyncTestDelegate code)
Parameters
expectedExceptionType
TypeThe expected Exception Type
code
AsyncTestDelegateA TestDelegate
Returns
CatchAsync(Type, AsyncTestDelegate, string, params object?[]?)
Verifies that an async delegate throws an exception of a certain Type or one derived from it when called and returns it. The returned exception may be null when inside a multiple assert block.
public static Exception? CatchAsync(Type expectedExceptionType, AsyncTestDelegate code, string message, params object?[]? args)
Parameters
expectedExceptionType
TypeThe expected Exception Type
code
AsyncTestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
CatchAsync<TActual>(AsyncTestDelegate)
Verifies that an async delegate throws an exception of a certain Type or one derived from it when called and returns it. The returned exception may be null when inside a multiple assert block.
public static TActual? CatchAsync<TActual>(AsyncTestDelegate code) where TActual : Exception
Parameters
code
AsyncTestDelegateA TestDelegate
Returns
- TActual
Type Parameters
TActual
CatchAsync<TActual>(AsyncTestDelegate, string, params object?[]?)
Verifies that an async delegate throws an exception of a certain Type or one derived from it when called and returns it. The returned exception may be null when inside a multiple assert block.
public static TActual? CatchAsync<TActual>(AsyncTestDelegate code, string message, params object?[]? args) where TActual : Exception
Parameters
code
AsyncTestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
- TActual
Type Parameters
TActual
Catch<TActual>(TestDelegate)
Verifies that a delegate throws an exception of a certain Type or one derived from it when called and returns it. The returned exception may be null when inside a multiple assert block.
public static TActual? Catch<TActual>(TestDelegate code) where TActual : Exception
Parameters
code
TestDelegateA TestDelegate
Returns
- TActual
Type Parameters
TActual
Catch<TActual>(TestDelegate, string, params object?[]?)
Verifies that a delegate throws an exception of a certain Type or one derived from it when called and returns it. The returned exception may be null when inside a multiple assert block.
public static TActual? Catch<TActual>(TestDelegate code, string message, params object?[]? args) where TActual : Exception
Parameters
code
TestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
- TActual
Type Parameters
TActual
Charlie()
An alias of the corresponding Assert.Pass() method. Charlie Poole was the lead of NUnit for 21 years, across at least 207 releases in 37 different repositories, authoring 4,898 commits across them. He participated in 2,990 issues, 1,305 PRs, and impacted 6,992,983 lines of code. NUnit was downloaded from NuGet 225+ million times during his tenure. And those are only the numbers ones we can easily find; our numbers are sourced from after NUnit moved the project to GitHub in 2011, which means there are at least 9 additional years of work not quantified above.
This assertion attempts to pay homage to Charlie, who by virtue of his contributions has helped untold millions of tests pass.
public static void Charlie()
DoesNotThrow(TestDelegate)
Verifies that a delegate does not throw an exception.
public static void DoesNotThrow(TestDelegate code)
Parameters
code
TestDelegateA TestDelegate
DoesNotThrow(TestDelegate, string, params object?[]?)
Verifies that a delegate does not throw an exception
public static void DoesNotThrow(TestDelegate code, string message, params object?[]? args)
Parameters
code
TestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
DoesNotThrowAsync(AsyncTestDelegate)
Verifies that an async delegate does not throw an exception.
public static void DoesNotThrowAsync(AsyncTestDelegate code)
Parameters
code
AsyncTestDelegateA TestDelegate
DoesNotThrowAsync(AsyncTestDelegate, string, params object?[]?)
Verifies that an async delegate does not throw an exception
public static void DoesNotThrowAsync(AsyncTestDelegate code, string message, params object?[]? args)
Parameters
code
AsyncTestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Fail()
Marks the test as failed. Returns without throwing an exception when inside a multiple assert block.
public static void Fail()
Fail(string)
Marks the test as failed with the message and arguments that are passed in. Returns without throwing an exception when inside a multiple assert block.
public static void Fail(string message)
Parameters
message
stringThe message to initialize the AssertionException with.
Ignore()
Throws an IgnoreException. This causes the test to be reported as ignored.
public static void Ignore()
Ignore(string)
Throws an IgnoreException with the message and arguments that are passed in. This causes the test to be reported as ignored.
public static void Ignore(string message)
Parameters
message
stringThe message to initialize the AssertionException with.
Inconclusive()
Throws an InconclusiveException. This causes the test to be reported as Inconclusive.
public static void Inconclusive()
Inconclusive(string)
Throws an InconclusiveException with the message and arguments that are passed in. This causes the test to be reported as inconclusive.
public static void Inconclusive(string message)
Parameters
message
stringThe message to initialize the InconclusiveException with.
Multiple(AsyncTestDelegate)
Wraps code containing a series of assertions, which should all be executed, even if they fail. Failed results are saved and reported at the end of the code block.
public static void Multiple(AsyncTestDelegate testDelegate)
Parameters
testDelegate
AsyncTestDelegateA TestDelegate to be executed in Multiple Assertion mode.
Multiple(TestDelegate)
Wraps code containing a series of assertions, which should all be executed, even if they fail. Failed results are saved and reported at the end of the code block.
public static void Multiple(TestDelegate testDelegate)
Parameters
testDelegate
TestDelegateA TestDelegate to be executed in Multiple Assertion mode.
MultipleAsync(AsyncTestDelegate)
Wraps code containing a series of assertions, which should all be executed, even if they fail. Failed results are saved and reported at the end of the code block.
public static Task MultipleAsync(AsyncTestDelegate testDelegate)
Parameters
testDelegate
AsyncTestDelegateAn AsyncTestDelegate to be executed in Multiple Assertion mode.
Returns
Pass()
Throws a SuccessException with the message and arguments that are passed in. This allows a test to be cut short, with a result of success returned to NUnit.
public static void Pass()
Pass(string)
Throws a SuccessException with the message and arguments that are passed in. This allows a test to be cut short, with a result of success returned to NUnit.
public static void Pass(string message)
Parameters
message
stringThe message to initialize the AssertionException with.
That(TestDelegate, IResolveConstraint, NUnitString, string, string)
Apply a constraint to a delegate. Returns without throwing an exception when inside a multiple assert block.
public static void That(TestDelegate code, IResolveConstraint constraint, NUnitString message = default, string actualExpression = "", string constraintExpression = "")
Parameters
code
TestDelegateA TestDelegate to be executed
constraint
IResolveConstraintA Constraint expression to be applied
message
NUnitStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
That(TestDelegate, IResolveConstraint, FormattableString, string, string)
Apply a constraint to a delegate. Returns without throwing an exception when inside a multiple assert block.
public static void That(TestDelegate code, IResolveConstraint constraint, FormattableString message, string actualExpression = "", string constraintExpression = "")
Parameters
code
TestDelegateA TestDelegate to be executed
constraint
IResolveConstraintA Constraint expression to be applied
message
FormattableStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
That(TestDelegate, IResolveConstraint, Func<string>, string, string)
Apply a constraint to a delegate. Returns without throwing an exception when inside a multiple assert block.
public static void That(TestDelegate code, IResolveConstraint constraint, Func<string> getExceptionMessage, string actualExpression = "", string constraintExpression = "")
Parameters
code
TestDelegateA TestDelegate to be executed
constraint
IResolveConstraintA Constraint expression to be applied
getExceptionMessage
Func<string>A function to build the message included with the Exception
actualExpression
stringconstraintExpression
string
That(bool, NUnitString, string)
Asserts that a condition is true. Returns without throwing an exception when inside a multiple assert block.
public static void That(bool condition, NUnitString message = default, string actualExpression = "")
Parameters
condition
boolThe evaluated condition
message
NUnitStringThe message to display if the condition is false
actualExpression
string
That(bool, FormattableString, string)
Asserts that a condition is true. Returns without throwing an exception when inside a multiple assert block.
public static void That(bool condition, FormattableString message, string actualExpression = "")
Parameters
condition
boolThe evaluated condition
message
FormattableStringThe message to display if the condition is false
actualExpression
string
That(bool, Func<string>, string)
Asserts that a condition is true. Returns without throwing an exception when inside a multiple assert block.
public static void That(bool condition, Func<string> getExceptionMessage, string actualExpression = "")
Parameters
condition
boolThe evaluated condition
getExceptionMessage
Func<string>A function to build the message included with the Exception
actualExpression
string
That(Func<bool>, NUnitString, string)
Asserts that a condition is true. Returns without throwing an exception when inside a multiple assert block.
public static void That(Func<bool> condition, NUnitString message = default, string actualExpression = "")
Parameters
condition
Func<bool>A lambda that returns a Boolean
message
NUnitStringThe message to display if the condition is false
actualExpression
string
That(Func<bool>, FormattableString, string)
Asserts that a condition is true. Returns without throwing an exception when inside a multiple assert block.
public static void That(Func<bool> condition, FormattableString message, string actualExpression = "")
Parameters
condition
Func<bool>A lambda that returns a Boolean
message
FormattableStringThe message to display if the condition is false
actualExpression
string
That(Func<bool>, Func<string>, string)
Asserts that a condition is true. Returns without throwing an exception when inside a multiple assert block.
public static void That(Func<bool> condition, Func<string> getExceptionMessage, string actualExpression = "")
Parameters
condition
Func<bool>A lambda that returns a Boolean
getExceptionMessage
Func<string>A function to build the message included with the Exception
actualExpression
string
ThatAsync(AsyncTestDelegate, IResolveConstraint, NUnitString, string, string)
Apply a constraint to an async delegate. Returns without throwing an exception when inside a multiple assert block.
public static Task ThatAsync(AsyncTestDelegate code, IResolveConstraint constraint, NUnitString message = default, string actualExpression = "", string constraintExpression = "")
Parameters
code
AsyncTestDelegateAn AsyncTestDelegate to be executed
constraint
IResolveConstraintA Constraint expression to be applied
message
NUnitStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Returns
- Task
Awaitable.
ThatAsync(AsyncTestDelegate, IResolveConstraint, FormattableString, string, string)
Apply a constraint to an async delegate. Returns without throwing an exception when inside a multiple assert block.
public static Task ThatAsync(AsyncTestDelegate code, IResolveConstraint constraint, FormattableString message, string actualExpression = "", string constraintExpression = "")
Parameters
code
AsyncTestDelegateAn AsyncTestDelegate to be executed
constraint
IResolveConstraintA Constraint expression to be applied
message
FormattableStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Returns
- Task
Awaitable.
ThatAsync<T>(Func<Task<T>>, IResolveConstraint, NUnitString, string, string)
Apply a constraint to an async delegate. Returns without throwing an exception when inside a multiple assert block.
public static Task ThatAsync<T>(Func<Task<T>> code, IResolveConstraint constraint, NUnitString message = default, string actualExpression = "", string constraintExpression = "")
Parameters
code
Func<Task<T>>An async method to be executed
constraint
IResolveConstraintA Constraint expression to be applied
message
NUnitStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Returns
- Task
Awaitable.
Type Parameters
T
ThatAsync<T>(Func<Task<T>>, IResolveConstraint, FormattableString, string, string)
Apply a constraint to an async delegate. Returns without throwing an exception when inside a multiple assert block.
public static Task ThatAsync<T>(Func<Task<T>> code, IResolveConstraint constraint, FormattableString message, string actualExpression = "", string constraintExpression = "")
Parameters
code
Func<Task<T>>An async method to be executed
constraint
IResolveConstraintA Constraint expression to be applied
message
FormattableStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Returns
- Task
Awaitable.
Type Parameters
T
That<TActual>(ActualValueDelegate<TActual>, IResolveConstraint, NUnitString, string, string)
Apply a constraint to a delegate. Returns without throwing an exception when inside a multiple assert block.
public static void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr, NUnitString message = default, string actualExpression = "", string constraintExpression = "")
Parameters
del
ActualValueDelegate<TActual>An ActualValueDelegate returning the value to be tested
expr
IResolveConstraintA Constraint expression to be applied
message
NUnitStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Type Parameters
TActual
The Type being compared.
That<TActual>(ActualValueDelegate<TActual>, IResolveConstraint, FormattableString, string, string)
Apply a constraint to a delegate. Returns without throwing an exception when inside a multiple assert block.
public static void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr, FormattableString message, string actualExpression = "", string constraintExpression = "")
Parameters
del
ActualValueDelegate<TActual>An ActualValueDelegate returning the value to be tested
expr
IResolveConstraintA Constraint expression to be applied
message
FormattableStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Type Parameters
TActual
The Type being compared.
That<TActual>(ActualValueDelegate<TActual>, IResolveConstraint, Func<string>, string, string)
Apply a constraint to a delegate. Returns without throwing an exception when inside a multiple assert block.
public static void That<TActual>(ActualValueDelegate<TActual> del, IResolveConstraint expr, Func<string> getExceptionMessage, string actualExpression = "", string constraintExpression = "")
Parameters
del
ActualValueDelegate<TActual>An ActualValueDelegate returning the value to be tested
expr
IResolveConstraintA Constraint expression to be applied
getExceptionMessage
Func<string>A function to build the message included with the Exception
actualExpression
stringconstraintExpression
string
Type Parameters
TActual
The Type being compared.
That<TActual>(TActual, IResolveConstraint, NUnitString, string, string)
Apply a constraint to an actual value. Returns without throwing an exception when inside a multiple assert block.
public static void That<TActual>(TActual actual, IResolveConstraint expression, NUnitString message = default, string actualExpression = "", string constraintExpression = "")
Parameters
actual
TActualThe actual value to test
expression
IResolveConstraintA Constraint expression to be applied
message
NUnitStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Type Parameters
TActual
The Type being compared.
That<TActual>(TActual, IResolveConstraint, FormattableString, string, string)
Apply a constraint to an actual value. Returns without throwing an exception when inside a multiple assert block.
public static void That<TActual>(TActual actual, IResolveConstraint expression, FormattableString message, string actualExpression = "", string constraintExpression = "")
Parameters
actual
TActualThe actual value to test
expression
IResolveConstraintA Constraint expression to be applied
message
FormattableStringThe message that will be displayed on failure
actualExpression
stringconstraintExpression
string
Type Parameters
TActual
The Type being compared.
That<TActual>(TActual, IResolveConstraint, Func<string>, string, string)
Apply a constraint to an actual value. Returns without throwing an exception when inside a multiple assert block.
public static void That<TActual>(TActual actual, IResolveConstraint expression, Func<string> getExceptionMessage, string actualExpression = "", string constraintExpression = "")
Parameters
actual
TActualThe actual value to test
expression
IResolveConstraintA Constraint expression to be applied
getExceptionMessage
Func<string>A function to build the message included with the Exception
actualExpression
stringconstraintExpression
string
Type Parameters
TActual
The Type being compared.
Throws(IResolveConstraint, TestDelegate)
Verifies that a delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static Exception? Throws(IResolveConstraint expression, TestDelegate code)
Parameters
expression
IResolveConstraintA constraint to be satisfied by the exception
code
TestDelegateA TestSnippet delegate
Returns
Throws(IResolveConstraint, TestDelegate, string, params object?[]?)
Verifies that a delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static Exception? Throws(IResolveConstraint expression, TestDelegate code, string message, params object?[]? args)
Parameters
expression
IResolveConstraintA constraint to be satisfied by the exception
code
TestDelegateA TestSnippet delegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
Throws(Type, TestDelegate)
Verifies that a delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static Exception? Throws(Type expectedExceptionType, TestDelegate code)
Parameters
expectedExceptionType
TypeThe exception Type expected
code
TestDelegateA TestDelegate
Returns
Throws(Type, TestDelegate, string, params object?[]?)
Verifies that a delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static Exception? Throws(Type expectedExceptionType, TestDelegate code, string message, params object?[]? args)
Parameters
expectedExceptionType
TypeThe exception Type expected
code
TestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
ThrowsAsync(IResolveConstraint, AsyncTestDelegate)
Verifies that an async delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static Exception? ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code)
Parameters
expression
IResolveConstraintA constraint to be satisfied by the exception
code
AsyncTestDelegateA TestSnippet delegate
Returns
ThrowsAsync(IResolveConstraint, AsyncTestDelegate, string, params object?[]?)
Verifies that an async delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static Exception? ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, string message, params object?[]? args)
Parameters
expression
IResolveConstraintA constraint to be satisfied by the exception
code
AsyncTestDelegateA TestSnippet delegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
ThrowsAsync(Type, AsyncTestDelegate)
Verifies that an async delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static Exception? ThrowsAsync(Type expectedExceptionType, AsyncTestDelegate code)
Parameters
expectedExceptionType
TypeThe exception Type expected
code
AsyncTestDelegateA TestDelegate
Returns
ThrowsAsync(Type, AsyncTestDelegate, string, params object?[]?)
Verifies that an async delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static Exception? ThrowsAsync(Type expectedExceptionType, AsyncTestDelegate code, string message, params object?[]? args)
Parameters
expectedExceptionType
TypeThe exception Type expected
code
AsyncTestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
ThrowsAsync<TActual>(AsyncTestDelegate)
Verifies that an async delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static TActual? ThrowsAsync<TActual>(AsyncTestDelegate code) where TActual : Exception
Parameters
code
AsyncTestDelegateA TestDelegate
Returns
- TActual
Type Parameters
TActual
Type of the expected exception
ThrowsAsync<TActual>(AsyncTestDelegate, string, params object?[]?)
Verifies that an async delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static TActual? ThrowsAsync<TActual>(AsyncTestDelegate code, string message, params object?[]? args) where TActual : Exception
Parameters
code
AsyncTestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
- TActual
Type Parameters
TActual
Type of the expected exception
Throws<TActual>(TestDelegate)
Verifies that a delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static TActual? Throws<TActual>(TestDelegate code) where TActual : Exception
Parameters
code
TestDelegateA TestDelegate
Returns
- TActual
Type Parameters
TActual
Type of the expected exception
Throws<TActual>(TestDelegate, string, params object?[]?)
Verifies that a delegate throws a particular exception when called. The returned exception may be null when inside a multiple assert block.
public static TActual? Throws<TActual>(TestDelegate code, string message, params object?[]? args) where TActual : Exception
Parameters
code
TestDelegateA TestDelegate
message
stringThe message that will be displayed on failure
args
object[]Arguments to be used in formatting the message
Returns
- TActual
Type Parameters
TActual
Type of the expected exception
Warn(string)
Issues a warning using the message and arguments provided.
public static void Warn(string message)
Parameters
message
stringThe message to display.