Emerica.
Emerica.

Reputation: 568

How do I use one SetUpTestFixture for mutiple classes?

Ok so I have the following code, that I want to run only once. I want it to setup at the very beggining of all of the testfixtures I run and once at the end.

 [RequiresSTA]
    class Prepare
    {
        [TestFixtureSetUp]
        public void Setup()
        {
            IE Window = new IE("http://www.google.com");
        }

        [TestFixtureTearDown]
        public void Teardown()
        {
            IE Window = IE.AttachTo<IE>(Find.ByTitle("Google"));
            Window.Dispose();
        }
    }

I have multiple classes in my project in the same namespace, lets call them Class1 - 4. I want to set it up so when I run these tests, Prepare only gets called once. Example:

Running 3 + 4:

Prepare Setup
     Run3
     Run4
Prepare Teardown

Running 1 - 4:

Prepare Setup
     Run1
     Run2
     Run3
     Run4
Prepare Teardown

Right now Prepare initiates each time a set begins and then concludes each time they end. How can I make it contiuos until all tests in that namespace have been ran?

Upvotes: 1

Views: 835

Answers (2)

AJ_Cemprola
AJ_Cemprola

Reputation: 143

Creating an Assembly setup fixture would be the best way.

    //This class does not have a namespace on purpose, do not add one.
  [SetUpFixture]
  public class AssemblySetupFixture
  {
    [SetUp]
    public void SetupTestSuite()
    {
        Debug.WriteLine("Setting up Test Assembly Fixture");
     // Kill all web server instances and then restart them
        WebServerHelper.StopAll(); 
        WebServerHelper.StartAll();
       DatabaseHelper.RestoreDefaultDatabases();
     }

    [TearDown]
    public void TearDownTestSuite()
    {
        Debug.WriteLine("Tearing down Assembly Fixture");
        WebServerHelper.StopAll();
    }
  }

Upvotes: 1

Justin Pihony
Justin Pihony

Reputation: 67135

You can use inheritance for this. You can create a base class that all tests inherit from, and in the base class you can put your setups.

You have to be careful in doing this though. This will mask the setup to anybody who is looking at just the subclass. Unless you override the setup and at least call base.Setup in every class.

However, this will only help with code reuse. I do not believe that this will work as you want, and should not. This setup is supposed to run once for every test class. In your unit tests you should not have dependencies on previous tests, so if you NEED this then you might want to re-look at your tests. They should work on their own without any previous tests having to have run. If you do that, then having each class set up its own fixture should not be a problem.

The only way around this would be to have all your tests in one fixture, which, again would be bad test design.

UPDATE

It does appear that you can use the SetupFixture attribute on a global class that will run for the entire assembly. As stated above, though, use this very carefully as you typically do not want shared state in unit tests.

Something noteworthy to pull from the above link is this:

Only one SetUpFixture should be created in a given namespace. A SetUpFixture outside of any namespace provides SetUp and TearDown for the entire assembly.

Upvotes: 1

Related Questions