Table of Contents

Class Assert

Namespace
NUnit.Framework
Assembly
nunit.framework.dll

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 object

The actual value to test

expression IResolveConstraint

A Constraint expression to be applied

message string

The message that will be displayed on failure

actualExpression string
constraintExpression 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 TestDelegate

A TestDelegate

Returns

Exception

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 TestDelegate

A TestDelegate

message string

The message that will be displayed on failure

args object[]

Arguments to be used in formatting the message

Returns

Exception

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 Type

The expected Exception Type

code TestDelegate

A TestDelegate

Returns

Exception

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 Type

The expected Exception Type

code TestDelegate

A TestDelegate

message string

The message that will be displayed on failure

args object[]

Arguments to be used in formatting the message

Returns

Exception

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 AsyncTestDelegate

A TestDelegate

Returns

Exception

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 AsyncTestDelegate

A TestDelegate

message string

The message that will be displayed on failure

args object[]

Arguments to be used in formatting the message

Returns

Exception

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 Type

The expected Exception Type

code AsyncTestDelegate

A TestDelegate

Returns

Exception

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 Type

The expected Exception Type

code AsyncTestDelegate

A TestDelegate

message string

The message that will be displayed on failure

args object[]

Arguments to be used in formatting the message

Returns

Exception

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 AsyncTestDelegate

A 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 AsyncTestDelegate

A TestDelegate

message string

The 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 TestDelegate

A 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 TestDelegate

A TestDelegate

message string

The 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 TestDelegate

A 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 TestDelegate

A TestDelegate

message string

The 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 AsyncTestDelegate

A 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 AsyncTestDelegate

A TestDelegate

message string

The 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 string

The 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 string

The 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 string

The 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 AsyncTestDelegate

A 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 TestDelegate

A 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 AsyncTestDelegate

An AsyncTestDelegate to be executed in Multiple Assertion mode.

Returns

Task

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 string

The 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 TestDelegate

A TestDelegate to be executed

constraint IResolveConstraint

A Constraint expression to be applied

message NUnitString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 TestDelegate

A TestDelegate to be executed

constraint IResolveConstraint

A Constraint expression to be applied

message FormattableString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 TestDelegate

A TestDelegate to be executed

constraint IResolveConstraint

A Constraint expression to be applied

getExceptionMessage Func<string>

A function to build the message included with the Exception

actualExpression string
constraintExpression 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 bool

The evaluated condition

message NUnitString

The 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 bool

The evaluated condition

message FormattableString

The 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 bool

The 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 NUnitString

The 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 FormattableString

The 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 AsyncTestDelegate

An AsyncTestDelegate to be executed

constraint IResolveConstraint

A Constraint expression to be applied

message NUnitString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 AsyncTestDelegate

An AsyncTestDelegate to be executed

constraint IResolveConstraint

A Constraint expression to be applied

message FormattableString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 IResolveConstraint

A Constraint expression to be applied

message NUnitString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 IResolveConstraint

A Constraint expression to be applied

message FormattableString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 IResolveConstraint

A Constraint expression to be applied

message NUnitString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 IResolveConstraint

A Constraint expression to be applied

message FormattableString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 IResolveConstraint

A Constraint expression to be applied

getExceptionMessage Func<string>

A function to build the message included with the Exception

actualExpression string
constraintExpression 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 TActual

The actual value to test

expression IResolveConstraint

A Constraint expression to be applied

message NUnitString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 TActual

The actual value to test

expression IResolveConstraint

A Constraint expression to be applied

message FormattableString

The message that will be displayed on failure

actualExpression string
constraintExpression 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 TActual

The actual value to test

expression IResolveConstraint

A Constraint expression to be applied

getExceptionMessage Func<string>

A function to build the message included with the Exception

actualExpression string
constraintExpression 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 IResolveConstraint

A constraint to be satisfied by the exception

code TestDelegate

A TestSnippet delegate

Returns

Exception

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 IResolveConstraint

A constraint to be satisfied by the exception

code TestDelegate

A TestSnippet delegate

message string

The message that will be displayed on failure

args object[]

Arguments to be used in formatting the message

Returns

Exception

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 Type

The exception Type expected

code TestDelegate

A TestDelegate

Returns

Exception

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 Type

The exception Type expected

code TestDelegate

A TestDelegate

message string

The message that will be displayed on failure

args object[]

Arguments to be used in formatting the message

Returns

Exception

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 IResolveConstraint

A constraint to be satisfied by the exception

code AsyncTestDelegate

A TestSnippet delegate

Returns

Exception

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 IResolveConstraint

A constraint to be satisfied by the exception

code AsyncTestDelegate

A TestSnippet delegate

message string

The message that will be displayed on failure

args object[]

Arguments to be used in formatting the message

Returns

Exception

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 Type

The exception Type expected

code AsyncTestDelegate

A TestDelegate

Returns

Exception

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 Type

The exception Type expected

code AsyncTestDelegate

A TestDelegate

message string

The message that will be displayed on failure

args object[]

Arguments to be used in formatting the message

Returns

Exception

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 AsyncTestDelegate

A 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 AsyncTestDelegate

A TestDelegate

message string

The 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 TestDelegate

A 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 TestDelegate

A TestDelegate

message string

The 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 string

The message to display.