Parametrised Tests in NUnit

Parametrised tests help you to create many test cases without writing a lot of test methods. This approach saves you a lot of time and increases the quality of your tests. Let us look how we can parametrise our tests in NUnit.

 

The [TestCase] attribute

A parametrised test in NUnit needs the [TestCase] attribute, a test method with parameters and the values we want to pass to the test:

The example above passes in values 1 for a, 2 for b and 3 for the expected result. We can run the test and it passes.

As long as we only have one test case, parametrised tests offer us not much help. This changes as soon as we have multiple input combinations. We now can add more [TestCase] attributes to the same test:

We still use the same test method, but we now have 6 test cases. In the Test Explorer window we can see the different tests and their input parameters:

The test explorer lists all our test cases

Since these are separate test cases, we know exactly which combination failed should some tests no longer pass.

 

Next

Parametrised tests help you a lot with testing multiple input combinations to your methods. You only need to write one test and hand in the different test values you want to check. Next week we take a look at how we can reuse the values we put into our parametrised tests.

1 thought on “Parametrised Tests in NUnit”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.