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 database 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.
The
[SetUpFixture]attribute allows you to run setup and/or teardown code once for all tests under the same namespace.Here is the documentation on
SetUpFixture. According to the documentation:So if you need
SetUpandTearDownfor all tests, then just make sure theSetUpFixtureclass is not in a namespace.Alternatively, you could always define a static class strictly for the purpose of defining “global” test variables.