Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to parameterize a nunit test

Tags:

c#

nunit

I would like to write a callable function that accepts two objects, and compares 30+ properties of those objects with asserts. The issue is this needs to be done for about 20 existing unit tests and most future tests, and writing out the 30+ asserts each time is both time and space consuming.

I currently have a non unit test function that compares the objects, and returns a string with "pass" or a failure message, and use an assert to validate that in each unit test. However, its quite messy and I feel like I'm going against proper unit testing methods.

Is there a way to make a function that is callable from inside unit tests that uses asserts to check conditions?

like image 723
NewNetProgrammer Avatar asked Jan 27 '11 15:01

NewNetProgrammer


People also ask

How do you parameterize a TestCase?

There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized. class). Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.

Does NUnit have data provider?

All of the built-in attributes allow for data to be provided programmatically, but NUnit does not have any attributes that fetch the data from a file or other external source.

How can you make a NUnit TestCase Datadriven?

The first way to create data driven tests is by using the [TestCase] attribute that NUnit provides. You can add multiple [TestCase] attributes for a single test method, and specify the combinations of input and expected output parameters that the test method should take.


1 Answers

If you are using NUnit 2.5.5 or above, this is possible using the TestCase attribute.

Normal unit tests would be decorated with [Test], but we can replace that as follows:

[TestCase("0", 1)] [TestCase("1", 1)] [TestCase("2", 1)] public void UnitTestName(string input, int expected) {     //Arrange      //Act      //Assert } 

That type of thing will be the way to do it - obviously take different params.

Look at this for help: http://nunit.org/?p=testCase&r=2.5

like image 83
stack72 Avatar answered Sep 18 '22 19:09

stack72