tags:

views:

69

answers:

2

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?

+1  A: 

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...

Thomas Levesque
Singleton's have their place. This might just be one. :)
Robert P
Yes, but only if he has control over the Metrics class...
Thomas Levesque
+3  A: 

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."

TrueWill
Looks like this is the nUnit way. Thanks!
Robert P
Nice, I didn't know this attribute...
Thomas Levesque