Alex Rivera | Logout

UnitTesting Static Classes

Asked 2009-06-10T03:25:56.053
22

Scenario. Language C#, Unit testing using VS2008 Unit testing framework

I have a static class with a static constructor and 2 methods. I have 4 test methods written to test the entire class. My Static Constructor has some important initializations.

Now if I run all the 4 unit test cases in tandem, the static constructor will be called only at the beginning. At the end of each test case, there is no such thing

called static destructor, So the state info in the constructor gets carried to the next unit test case also. What is the workaround for this.

Edit
Report

2 Answers

0

Well, you didn't specify which language you were using, but if there is a way to open up your static class from within the test file, then I would add a fake destructor to it, which you can call after each test. That way, the 'destructor' stays in the test class, and out of your production code.

answered 2009-06-10T03:31:16.467
0

You could use Typemock's Isolator which is capable of mocking static classes, so in each test you can "define" how the static will be operating.

It's not a free product though.

answered 2009-06-10T03:39:58.923

Your Answer