Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have deterministic ClassCleanup in MSTest / VS Unit testing?

i.e. to have the method decorated with ClassCleanup attribute executed once immediately after all tests in a test class ? (like TestFixtureTearDown in NUnit). Any workarounds ?

The alternative of embedding this heavy setup/teardown in TestInitialize and TestCleanup would just drive up the test execution times.

I was comparing the two over the past week. (Here's what I found NUnit vs MSTest . Don't have a lot of flying time in MSTest, so if I've made a mistake, please feel free to post corrections as comments..)

This particular item is a showstopper as far as I am concerned. Read the first section of the blog post in case you'd like more details.

like image 321
Gishu Avatar asked Nov 13 '22 14:11

Gishu


1 Answers

ClassInitialize and ClassCleanup are called right after your class is loaded and right before your class is unloaded.

It's true, but classes are bulk-unloaded after all tests finished running.

E.g.

ClassInitialize1
TestInitialize1
TestMethod11
TestCleanup1
TestInitialize1
TestMethod12
TestCleanup1
ClassInitialize2
TestInitialize2
TestMethod21
TestCleanup2
TestInitialize2
TestMethod22
TestCleanup2
TestInitialize2
TestMethod23
TestCleanup2
ClassCleanup1
ClassCleanup2

like image 187
h49 Avatar answered Jan 12 '23 10:01

h49