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?
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.
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.
NUnit version 3 will support running tests in parallel: Adding the attribute to a class: [Parallelizable(ParallelScope. Self)] will run your tests in parallel.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With