At its simplest, a unit test consists of three stages:
These three stages are often called 'Arrange-Act-Assert', or 'Given-When-Then'.
Below is example in C# that uses the NUnit framework.
[TestFixture]
public CalculatorTest
{
[Test]
public void Add_PassSevenAndThree_ExpectTen()
{
// Arrange - setup environment
var systemUnderTest = new Calculator();
// Act - Call system under test
var calculatedSum = systemUnderTest.Add(7, 3);
// Assert - Validate expected result
Assert.AreEqual(10, calculatedSum);
}
}
Where necessary, an optional fourth clean up stage tidies up.