1

All tests create same "root object" in my system and the "root object" has multiple modes. I need each test to check everything against each mode. So I decided to make an internal static property in the "root object" where I can override which mode is used when it's created.

Now tests behavior depends on environment settings. I have a pretty big amount of tests and I can't modify each of them to test the same thing against each possible mode.

Instead I would like to setup modes in one place.

I want all the tests to run one time for each statically set mode when I hit "Run All Tests" in R#.

Is it possible to do?

Vlad
  • 3,001
  • 1
  • 22
  • 52
  • I think you should be able to solve it with this: http://stackoverflow.com/questions/11667868/visual-studio-parameterized-unit-test-like-java – gregkalapos Feb 01 '16 at 09:48
  • @gregkalapos I can't specify different TestCases for each test. I have too many tests. I need "global-scope" TestCases. – Vlad Feb 01 '16 at 09:50

1 Answers1

0

You can do this in NUnit by creating a SetUpFixture at the desired level of the namespace hierarchy defined by your tests. The OneTimeSetUp method of that fixture prepares the environment and the OneTimeTearDowm method cleans it up if necessary. If you want to do it for your entire assembly, just define the class with the SetUpFixtureAttribute outside of any namespace.

[SetUpFixture]
public static class MySetUpFixture
{
    [OneTimeSetUp]
    public static void SetUpTestEnvironment()
    {
        // Set up the environment, possibly leaving information
        // for the tests to use in static fields or properties.
    }

    [OneTimeTearDown]
    public static void CleanUpEnvironment()
    {
        // If any cleanup is needed, do it here
    }
}

The class and methods do not have to be static, but using static will keep you in mind of the fact that there is no communication between SetUpFixture instances and TestFixture instances.

Your tests can simply use the environment as created by the SetUpFixture or can query static properties of the class to change their behavior.

Charlie
  • 12,928
  • 1
  • 27
  • 31
  • 1. What's the difference between `[SetUp]` in `SetUpFixture` and `OneTimeSetUp`? As I understand both are executed once before any of the tests contained in its namespace. – Vlad Feb 01 '16 at 23:08
  • 2. This will allow to setup only one configuration but I need to also run my tests in other configurations too. – Vlad Feb 01 '16 at 23:09
  • It depends whether you are using NUnit V2 or V3. OneTimeSetUp is the attribute used in V3. – Charlie Feb 02 '16 at 00:49
  • My assumption for multiple setups was that they would be done in multiple runs of NUnit and that the SetUpFixture would be figuring out what environment to set up from environment variables, a file, etc. – Charlie Feb 02 '16 at 00:50
  • You've given me a feature idea however - parameterized SetUpFixtures! – Charlie Feb 02 '16 at 00:51
  • yes, I would like to have parametrized SetUpFixtures.. by now it looks like I have to use SharedProject and create a separate project with defines for each possible configuration combination. And what if I have 3 main test projects? x4 combinations = 12 projects + 3 shared, - just for testing. And I need to specify InternalsVisibleTo for them! – Vlad Feb 02 '16 at 00:54