NUnit Unit Testing Framework Cheat Sheet
NUnit Unit Testing Framework Cheat Sheet
Installation Attributes
using NUnit.Framework;
namespace NUnitUnitTests
{ Triggered before
// A class that contains NUnit unit tests. (Required)
[SetUp] [TestInitialize] Constructor
every test case.
[TestFixture]
public class NonBellatrixTests
{
[OneTimeSetUp] Triggered after
public void ClassInit() [TearDown] [TestCleanup] IDisposable.Dispose
{ every test case.
// Executes once for the test class. (Optional)
}
[SetUp]
public void TestInit() One-time triggered
{
// Runs before each test. (Optional) [OneTimeSetUp] [ClassInitialize] IClassFixture<T> method before test
}
cases start.
[Test]
public void TestMethod()
{
}
[TearDown]
public void TestCleanup()
One-time triggered
{ [OneTimeTearDown] [ClassCleanup] IClassFixture<T> method after test
// Runs after each test. (Optional)
}
cases end.
[OneTimeTearDown]
public void ClassCleanup()
{
// Runs once after all tests in this class are [Fact(Skip="reason"
executed. (Optional) [Ignore("reason")] [Ignore] Ignores a test case.
// Not guaranteed that it executes instantly after all )]
tests from the class.
}
}
} Sets arbitrary
// A SetUpFixture outside of any namespace provides [Property] [TestProperty] [Trait]
SetUp and TearDown for the entire assembly. metadata on a test.
[SetUpFixture]
public class MySetUpClass
{
[OneTimeSetUp] Configures a
public void RunBeforeAnyTests() [Theory] [DataRow] [Theory]
{ data-driven test.
// Executes once before the test run. (Optional)
}
[OneTimeTearDown]
Categorizes the
public void RunAfterAnyTests()
[Trait("Category",
{ [Category("")] [TestCategory("") test cases or
// Executes once after the test run. (Optional) ] "")]
}
classes.
}
[TestFixture] [Test]
[Author("Joro Doev", "[email protected]")] [Repeat(10)]
public class RocketFuelTests public void RocketFuelMeassuredCorrectly_When_Flying() { /* ... */ }
{
[Test]
Combinatorial Attribute
public void RocketFuelMeassuredCorrectly_When_Landing() { /* ... */ }
[Test]
[Author("Ivan Penchev")]
public void RocketFuelMeassuredCorrectly_When_Flying() { /* ... */ }
[Test, Combinatorial]
}
public void CorrectFuelMeassured_When_X_Site([Values(1,2,3)] int x, [Values("A","B")] string s)
{
...
Pairwise Attribute
}
[Test, Pairwise]
public void ValidateLandingSiteOfRover_When_GoingToMars
([Values("a", "b", "c")] string a, [Values("+", "-")] string b, [Values("x", "y")] string c) Random Attribute
{
Debug.WriteLine("{0} {1} {2}", a, b, c);
[Test]
}
public void GenerateRandomLandingSiteOnMoon([Values(1,2,3)] int x, [Random(-1.0,
1.0, 5)] double d)
{
Range Attribute ...
}
[Test]
public void CalculateJupiterBaseLandingPoint([Values(1,2,3)] int x, [Range(0.2,0.6)] double y)
{ Retry Attribute
//...
}
[Test]
[Retry(3)]
public void CalculateJupiterBaseLandingPoint([Values(1,2,3)] int x, [Range(0.2,0.6)] double y)
{
Timeout Attribute
//...
}
[Test, Timeout(2000)]
public void FireRocketToProximaCentauri()
{
...
}