Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create tests in NUnit

Using Nunit, I want to be able to write a test fixture that will read all the filenames in a particular directory and create a test for each file.

I could quite easily write one test method that scans through the directory and just does all the tests, but when I run NUnit I want to be able to see each of the tests individually.

Is this even possible?

like image 519
Ray Avatar asked May 15 '09 03:05

Ray


People also ask

How do you write multiple test cases in NUnit?

If you add a second parameter with the Values attribute, for per value of the first parameter, NUnit will add a test for every value of the second parameter. Another way to avoid having to write duplicate tests when testing the same behaviour with different inputs is to use TestCase attribute on the test itself.

What is TestFixture NUnit?

The [TestFixture] attribute denotes a class that contains unit tests. The [Test] attribute indicates a method is a test method. Save this file and execute the dotnet test command to build the tests and the class library and run the tests. The NUnit test runner contains the program entry point to run your tests.

How do you run a parallel test in NUnit?

NUnit version 3 will support running tests in parallel: Adding the attribute to a class: [Parallelizable(ParallelScope. Self)] will run your tests in parallel.


1 Answers

I've found a way that fits my purposes

Have one test case, and mark it with the TestCaseSource attribute like so

[Test, TestCaseSource("GetTestCases")] public void TestFile(string filename) {     //do test } 

Then write the GetTestCases to read all the file names in the directory

private static string[] GetTestCases() {     return GetAllFilesInCurrentDirectory(); } 

Then when I start NUnit I get a list of the tests to be run (listed under TestFile).

like image 139
Ray Avatar answered Sep 26 '22 11:09

Ray