Quick question, I’m using the Visual Studio’s testing framework for unit testing. Just wondering what’s the difference between using the constructor to do initialization work vs. having a method with [TestInitialize()] attribute?
Quick question, I’m using the Visual Studio’s testing framework for unit testing. Just wondering
Share
This post gives an overview of the different methods. As you can see, the ctor is called immediately before the
ClassInitialize(only once, of course) andTestInitialize.So put stuff that requires code in
ClassInitializein yourTestInitializemethod. Everything that should be set up beforeClassInitializegoes in the ctor.Obviously,
TestInitializecontent will be executed once before each test. The corresponding method to close after each test isTestCleanup. For classes, useClassCleanup. The same thing exists for assemblies as well (AssemblyInitialize/Cleanup).Further reading