As MSDN states the ClassInitializeAttribute
Identifies a method that contains code that must be used before any of the tests in the test class have run and to allocate resources to be used by the test class. This class cannot be inherited.
one example that I can think about where this can come in hand, is when you have a static field in your class that the constructor of the instances depends on.
class foo
{
static someObject bar;
int foobar;
public foo()
{
this.foobar = foo.bar.SomeMethod()
}
}
this way in your ClassInitializeAttribute
method you can assign a value to the static bar
object, which will effect all the instances created later on.
Another case you might want to use the ClassInitializeAttribute
is for assigning global objects that the test might use (such as a mock database etc.)