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.
The simplest solution is to add a “Reset” method to your static class, which would have the equivalent behaviour of destructing it and reconstructing it.
There may be a valid reason why you are using a static class here. However, because statics don’t play nicely with unit tests, I usually search for an alternative design.