NUnit is an open-source unit-testing framework for any .NET language.
A project using NUnit has several components:
nunit.framework.dll
— contains the basic typesIn NUnit, there are 2 main constructs: test fixtures and test methods.
using NUnit.Framework;
**[TestFixture]**
public class ClassTests {
**[Test]**
public void **MethodName_Scenario_ExpectedBehaviour**() {
// Arrange
// Act
// Assert
}
}
NUnit provides an Assert
class and other helper classes like Has
, Is
, etc. that encourage you to write constraint model tests.
// Typical format:
Assert.That(value, constraint);
// Examples:
Assert.That(2, Is.GreaterThan(0));
// A failure message can be given as the third argument
Assert.That(
int.MaxValue + 100,
Is.GreaterThan(0),
"This should be a really big positive number"
);
NUnit provides the [TestCase]
attribute which lets you pass parameters to a test method.