The DotNetExtras.Testing library implements common assertion methods that can be used in unit tests. For the detailed description of the library API, code samples, and usage scenarios, see the API documentation section.
The DotNetExtras.Testing library originated as an alternative to the FluentAssertions, after the latter switched to commercial licensing. It is not as comprehensive, but the library covers the most common assertion cases.
DotNetExtras.Testing uses the xUnit assertion library and custom code to cover some cases not supported by xUnit. The list of supported assertions includes the following methods:
BeEmpty
BeFalse
BeGreaterThan
BeGreaterThanOrEqual
BeNull
BeNullOrEmpty
BeOfType
BeTrue
Contain
ContainAll
ContainAny
EndWith
Equal
Match
NotBeEmpty
NotBeNull
NotBeNullOrEmpty
NotBeOfType
NotContain
NotContainAll
NotContainAny
NotEndWith
NotEqual
NotMatch
NotStartWith
StartWith
The following examples illustrate how to use the DotNetExtras.Testing assertions:
using DotNetExtras.Testing.Assertions;
...
user?.Must().NotBeNull();
user?.Id?.Length?.Must().Equal(8);
user?.Enabled?.Must().BeTrue();
user?.Email?.Must().NotEndWith("@example.com");
user?.SocialAccounts?.Keys?.Must().NotBeNullOrEmpty();
user?.SocialAccounts?.Values?.Must().ContainAny(["github", "twitter", "facebook"]);
"Hello, world!".Must().Match("^HELLO", true);
new int[] intArray = [100, 200, 300];
intArray.Must().NotContainAny([400, 500]);
For the complete example and other samples covering additional scenarios, see the source code (and read the comments) of the unit test project.