ClassInitialize
is called once by MSTest before calling any of TestMethod
, see notes here . TestInitialize
is called once before each test method. MSTest creates a new instance of the test class for each call to TestMethod
. This is why ClassInitialize
is a static method.
I need to execute one class (not static) in a class. I also need to execute all testing methods in the same instance of the class.
Do you mean that you need to execute one method in TestClass
or the test class (the class you are actually testing)?
In any case, you can have a static member in TestClass
and initialize it once in ClassInitialize
. It will be created only once and will exist throughout the life of your tests. You can call the method only once. You can then use this single instance in each of your test methods.
One thing you need to know about is that MSTest can alternate tests from different classes. Therefore, if you have any global mutable state that is accessed from more than one ClassInitialize
(or test for that matter), unpredictable things may occur. For this reason, it is better to avoid statics.
The requirement that all methods must be executed in one instance is rather unusual. Perhaps there is a way to reorganize your code to eliminate this limitation?
source share