How do I run a test case with different environment variables in xUnit?

I would run the test case several times with different custom environment variables.

Example:

[Theory]
[InlineData(1)]
[InlineData(2)]
[MyEnvironmentData("foo", "bar", "baz")]
public void MyEnvironmentTest(int arg1)
{
  var foo=MyEnvironment.Get("foo");
  Assert.Contains(foo, new []{"bar", "baz"});
}

      

The test script must run four times:

  • arg1 = 1, MyEnvironment.Get ("Foo") = "bar"
  • arg1 = 1, MyEnvironment.Get ("Foo") = "baz"
  • arg1 = 2, MyEnvironment.Get ("Foo") = "bar"
  • arg1 = 2, MyEnvironment.Get ("Foo") = "baz"

There can also be many MyEnvironmentData attributes. In this case, all permutations must be tested.

I previously did this in xUnit.Net v1 using a custom TestClassCommand

one where I implemented my permutation logic in a method EnumerateTestCommands

and a custom TestCommand

one that will take care of setting environment variables before each test run and cleanup after. All other methods have been delegated to xUnit's own TestClassCommands and TestCommands.

What would be the best way to implement a similar setup in xUnit v2?

+3


source to share





All Articles