Nunit parameterized TestFixtures with parameters set at runtime?

I am interested in the possibility of creating several test cases with constructor arguments passed to it at run time by a static method or by a property that returns IEnumerable.

In Nunit 2.5, they introduced parameterized tests and test instruments. This allows you to write one test and run it with several inputs provided using the TestCase attribute, and write one test class and create several instances of it with different constructor arguments, respectively.

In addition to this, you can create several test cases based on the output of a property or method using the TestCaseSource attribute. This will use the output of the method / property that IEnumerable implements to create a set of test tables, one per object in the list. This is what I would like to do, but at the instrument level is not the test level.


Some prerequisites for my use:

I am testing a simulation software and a "simulation environment" that must be loaded (from a serialized object) before any simulations can be run. There are about 5 different sim types, so my test class has 5 test methods (one for each sim type). I use inheritance now (instead of parameterized devices) to run test cases in several (half a dozen or so) simulation environments that were taken from production data.

My problem is that in a recent attempt to increase the reach of the code, we automatically generated all possible combinations of modeling components, which led to the creation of more than 100 characters. I don’t want to create inherited classes for each of them, so instead I use TestCaseSource with a property that returns all workspaces in the folder and modifies the tests so that they (re) load the sim environment in the test itself for each test file.

Ideally, I would like to have one fixture for each simulation environment and determine how many / what they are at runtime. I know that I can do the first by hard-coding the paths of the sim environment into 100+ TestFixture attributes, can I do the latter?

+4
source share
1 answer

I emailed Nunit-Discuss a list about this and received below a response from Charlie Poole . In short, this is not yet possible, but future versions of Nunit are looking at them.

Hi,

Simply put, what you want but not here. Parameterized fixtures (as you discovered), limited in that you can only use arguments that are allowed in the attributes. We would like to have a method that allows you to use properties and methods, as for case testing, but devices are a little more complicated, since the type can be generic.

Have you considered using a generic fixture as a workaround? You could pass into the environment as Type (or Types) and any constructor arguments as non-type arguments. I don’t know your application, so take this with salt, but what about something like ...

[TestFixture(typeof(Environment1), 42, "string")] public class TestClass<T> { IEnvironment env; public TestClass(int n, string s) { env = new T( n, s); } ... } 

Please note that this is "maillistcode", so it may not work. :-)

Charlie

+5
source

All Articles