tags:

views:

37

answers:

2

Where should I place code that should only run once (and not once per class)? An example for this would be a statement that initializes the DB connection string. And I only need to run that once and I don't want to place a new method within each "TestFixture" class just to do that.

+6  A: 

The [SetupFixture] attribute allows you to run setup and/or teardown code once for all tests under the same namespace. If that is not sufficient (i.e. you have multiple namespaces for your tests), you could always define a static class strictly for the purpose of defining "global" test variables.

Here are the docs on SetupFixture:

http://www.nunit.org/index.php?p=setupFixture&r=2.5.5

Ben Hoffstein
that is exactly what I was looking for, thanks :)
a b
+1  A: 

Create a class (I call mine Config) and decorate it with the [SetUpFixture] attribute. The [SetUp] and [TearDown] methods in the class will run once.

[SetUpFixture]
public class Config
{
    [SetUp]
    public void SetUp()
    {
    }

    [TearDown]
    public void TearDown()
    {
    }
}
Jamie Ide