An essential part of every UI test framework is the usage of a unit testing framework. One of the most popular ones in the .NET world is NUnit. However, you cannot find a single place where you can get started with its syntax. So, I decided that it would be great to create a complete cheat sheet. I hope that you will find it useful. Enjoy!

Installation

To discover or execute test cases, VSTest would call the test adapters based on your project configuration. (That is why NUnit/xUnit/MSTest all ask you to install a test adapter NuGet package to your unit testing projects). So NUnit.TestAdapter exists for that purposes.



NUnit itself implements the testing frameworks and its contracts. So you need to add a NuGet reference to it to write unit test cases and have them compiled. Only compiled projects along with the test adapter can then be consumed by Visual Studio.

Initially, I created the cheat sheet while we developed the first versions of the BELLATRIX automated testing framework. Мost of the stuff in it are still relevant.

Test Execution Workflow

OneTimeSetUp from SetUpFixture (once per assembly)

OneTimeSetUp from TestFixture (once per test class class)

SetUp (before each test of the class)

Test1

TearDown (after each test of the class)

SetUp

Test2

TearDown

...

OneTimeTearDown from TestFixture (once per test class)

OneTimeSetUp from TestFixture

...

OneTimeTearDown from TestFixture

OneTimeTearDown from SetUpFixture (once per assembly)

Attributes Comparison

Comparing NUnit to other frameworks.

NUnit 3.x MSTest v2.x. xUnit.net 2.x Comments [Test] [TestMethod] [Fact] Marks a test method. [TestFixture] [TestClass] n/a Marks a test class. [SetUp] [TestInitialize] Constructor Triggered before every test case. [TearDown] [TestCleanup] IDisposable.Dispose Triggered after every test case. [OneTimeSetUp] [ClassInitialize] IClassFixture<T> One-time triggered method before test cases start. [OneTimeTearDown] [ClassCleanup] IClassFixture<T> One-time triggered method after test cases end. [Ignore("reason")] [Ignore] [Fact(Skip="reason")] Ignores a test case. [Property] [TestProperty] [Trait] Sets arbitrary metadata on a test. [Theory] [DataRow] [Theory] Configures a data-driven test. [Category("")] [TestCategory("")] [Trait("Category", "")] Categorizes the test cases or classes.

Assertions

Assertions- Classic Model

The classic Assert model uses a separate method to express each individual assertion of which it is capable.

Assertions- Constraint Model

The constraint-based Assert model uses a single method of the Assert class for all assertions. The logic necessary to carry out each assertion is embedded in the constraint object passed as the second parameter to that method. The second argument in this assertion uses one of NUnit's syntax helpers to create an EqualConstraint.

Advanced Attributes

Author Attribute The Author Attribute adds information about the author of the tests. It can be applied to test fixtures and to tests.

Repeat Attribute

RepeatAttribute is used on a test method to specify that it should be executed multiple times. If any repetition fails, the remaining ones are not run and a failure is reported.

Combinatorial Attribute

The CombinatorialAttribute is used on a test to specify that NUnit should generate test cases for all possible combinations of the individual data items provided for the parameters of a test.

Generated tests: CorrectFuelMeassured_When_X_Site(1, "A")

CorrectFuelMeassured_When_X_Site(1, "B")

CorrectFuelMeassured_When_X_Site(2, "A")

CorrectFuelMeassured_When_X_Site(2, "B")

CorrectFuelMeassured_When_X_Site(3, "A")

CorrectFuelMeassured_When_X_Site(3, "B")

Pairwise Attribute

The PairwiseAttribute is used on a test to specify that NUnit should generate test cases in such a way that all possible pairs of values are used.

Resulted pairs: a + y a - x b - y b + x c - x c + y

Random Attribute

The RandomAttribute is used to specify a set of random values to be provided for an individual numeric parameter of a parameterized test method. The following test will be executed fifteen times, three times for each value of x, each combined with 5 random doubles from -1.0 to +1.0.

Range Attribute

The RangeAttribute is used to specify a range of values to be provided for an individual parameter of a parameterized test method. NUnit creates test cases from all possible combinations of the provided on parameters - the combinatorial approach.

Generated tests: CalculateJupiterBaseLandingPoint(1, 0.2)

CalculateJupiterBaseLandingPoint(1, 0.4)

CalculateJupiterBaseLandingPoint(1, 0.6)

CalculateJupiterBaseLandingPoint(2, 0.2)

CalculateJupiterBaseLandingPoint(2, 0.4)

CalculateJupiterBaseLandingPoint(2, 0.6)

CalculateJupiterBaseLandingPoint(3, 0.2)

CalculateJupiterBaseLandingPoint(3, 0.4)

CalculateJupiterBaseLandingPoint(3, 0.6)

Retry Attribute

RetryAttribute is used on a test method to specify that it should be rerun if it fails, up to a maximum number of times.

Timeout Attribute

The TimeoutAttribute is used to specify a timeout value in milliseconds for a test case. If the test case runs longer than the time specified it is immediately cancelled and reported as a failure, with a message indicating that the timeout was exceeded.

Execute Tests in Parallel

Parallel execution of methods within a class is supported starting with NUnit 3.7. In earlier releases, parallel execution only applies down to the TestFixture level, ParallelScope.Childrenworks as ParallelScope.Fixtures and any ParallelizableAttribute placed on a method is ignored.