NUnit
NUnit uses a constraint model of assertions. The logic necessary to carry out each assertion is embedded in the constraint object passed as the second parameter to that method.
- Constraint model 1
- Constraints by category 2
- Install 3
- Visual Studio Test Generator 4
- Visual Studio Test Adapter 5
| NUnit | Assert.That |
|---|---|
| object type and properties | Is.TypeOf<T>(), Is.InstanceOf<T>(), Has.Property("PropertyName") |
| exceptions | Throws.Exception Throws.TypeOf<TException>() .With.Matches |
| strings | Is.EqualTo(“expected”) Is.EqualTo("eXpeCTed").IgnoreCase Is.Not.EqualTo(“notExpected”) |
| Numerical values | Is.EqualTo(int) Is.EqualTo(float).Within(tolerance) Is.EqualTo(val).Within(p).Percent Is.Positive / Is.Negative / Is.NaN |
| DateTime | Is.EqualTo(dt).Within(ts) Is.EqualTo(dt).Within(v).Milliseconds |
| Ranges | Is.GreaterThan(v) / Is.LessThan(v) Is.GreaterThanOrEqualTo(v) Is.InRange(lo,hi) |
| Collections | Is.All.Empty Contains.Item(item) Has.Some.ContainsSubstring(“sub_str”) Has.Exactly(m).EndsWith(“suffix”) Is.Unique Has.None.EqualTo(val) Is.EquivalentTo(actualCollection) Is.Ordered |
| References | Is.SameAs Is.Not.SameAs |
| Nulls & booleans | Is.Not.Empty, Is.Null, Is.True |
Official
Unit testing C# with NUnit and .NET Core 6
Explicit attribute 7
How to enable Trace and Debug output 8
Classic assertion model 9
CollectionEquivalentConstraint 10
CollectionContainsConstraint 11
Migration guidance to v4 12
SetUpFixture 13
Ignore attribute (v2) 14
Data-Driven Tests
NUnit parameterized tests tutorial with examples 15.
TestCase— constant argument valuesTestCaseSource— static method returningIEnumerable<TestCaseData>ValueSourceAttribute— Cartesian product of individual values- Test class constructor with parameters via
TestFixturearguments - Pass
ExpectedResulttoTestCase
Data-driven tests with NUnit 16
Dynamic test cases spec 17
Template-Based Test Naming 18
TestCaseData returns 19
Samples
[SetUpFixture] AND path:/^*.cs$/ AND nunit
Global setup of IServiceProvider 20
Create database for integration tests 21
default template
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>