Stupid NUnit Tricks
Being a natural factorer, I wrote a suite of tests to be run by NUnit using the following structure:
private void PerformStyleTest(string testFile, FontStyle expectedStyle)
{
// ...
// Check to see that expectedStyle matches the results
// ... boring code left out
}
[Test]
public void PlainText()
{
PerformStyleTest("PlainText.tif", (FontStyle)0);
}
[Test]
public void BoldText()
{
PerformStyleTest("BoldText.tif", FontStyle.Bold);
}
[Test]
public void ItalicText()
{
PerformStyleTest("ItalicText.tif", FontStyle.Italic);
}
This is nice, because it gives the illusion of granualr tests, but uses the same chunk of code for the actual test. This lets me isolate problems fairly easily.
Initially, I had a single object type that needed to be tested which I dutifully created in the [Setup] method and destroyed in the [Teardown] method.
I made some advances in our codebase that gave me fours classes that all descended from the same base class that needed to be tested through this rig.
Conveniently enough, I could make the base test class abstract, then the setup and teardown look like this:
[Setup]
public void Initialize()
{
_testingObject = CreateTestingObject();
}
[Teardown]
public void Shutdown()
{
DestoryTestingObject(_testingObject);
}
protected abstract TestingObject CreateTestingObject();
protected void DestroyTestingObject(TestingObject o);
So now I have individual files for each class of TestingObject (name changed to protect the innocent) which in turn will define a nice suite of unit tests for the whole shebang. The problem is that that it's hard to run an individual test through TestDriven .NET and the [Ignore] decoration will ignore that test for all instances. There are work arounds, but they're not as elegant as I'd like.