Reputation: 15968
I'm planning on implementing some tests using nUnit. I also have a homegrown metrics recording library that I'd like to use to use to get some metrics out of the test. Said metrics library is normally used in applications, and is usually used something like this:
// retrieve configuration.
string path = GetPath();
bool option = GetOption();
// Set up metrics recording object.
Metrics m = new Metrics(path, option);
Do.Something();
m.AddRecord(new Record()); // Record some metrics on what just happened
// later...
m.Finish(); // let it know we're done.
Now, I could create an instance of this object using TestFixtureSetup
and TestFixtureTeardown
in each object (such as in a parent test class), but I'd much rather have this metrics object persist across all tests run. Is it possible, and if so, what's necessary to do that?
Upvotes: 1
Views: 722
Reputation: 67
The other answers might work for older versions of NUnit, but for 4.0, the OneTimeSetUp attribute can be used on the setup method to have it run one time before any test rather than before each test.
Upvotes: 1
Reputation: 25523
You could use the SetUpFixtureAttribute - "marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace."
Upvotes: 3
Reputation: 292425
You could expose the Metrics object as a singleton, or simply as a static property of a helper class :
class TestHelper
{
public static Metrics Metrics { get; private set; }
static TestHelper
{
string path = GetPath();
bool option = GetOption();
Metrics = new Metrics(path, option);
}
}
OK, it's not a very nice pattern, but for unit tests it should be fine...
Upvotes: 1