1

I'm using [ClassInitialize] and [ClassCleanup] tags from Microsoft.VisualStudio.TestTools.UnitTesting. MSTest 2.0

I have a test class with tests that require my app to be in a different mode than usual.

I turn the mode on in [ClassInitialize] and off in [ClassCleanup].

This doesn't work, as I learned from a comment to this answer: there's no guarantee that the execution of [ClassCleanup]happens before any other tests are started. This is bad because in another class i have tests that require for the mode to be turned off. The mode doesn't get turned off in time and they fail.

I could turn the mode on/off for every test individually, but this is the very last option, because it takes a lot of time.

I could move the class to it's own assembly, but i don't really want to do that either.

I fixed it by turning the mode off in [ClassInitialize] of the class that requires it to be off.

But i need to know, does [ClassInitialize] share the same unintuitive behavior with [ClassCleanup], or is it guaranteed to NOT run before any previous tests are finished? That would make the first tests fail.

The very best solution would be to somehow force [ClassCleanup] to run immediately after the first test class is all finished, but that's probably not possible.

  • https://stackoverflow.com/questions/59971028/mstest-v2-execute-unittests-sequentially-donotparallelize – Hans Passant Jan 05 '23 at 13:46
  • Use Waitone : https://stackoverflow.com/questions/1544827/is-there-a-way-to-run-unit-tests-sequentially-with-mstests?force_isolation=true – jdweng Jan 05 '23 at 14:02
  • @HansPassant My tests are NOT executed in parallel, so neither [DoNotParallelize] nor Mutex helps. ClassCleanup execution order is just weird: http://web.archive.org/web/20150715221034/http://blogs.msdn.com/b/ploeh/archive/2007/01/06/classcleanupmayrunlaterthanyouthink.aspx The page is from 2007 but it still seems to be the case. I just hope it isn't right about executing tests from different classes interspersed. Then the only safe way would be to actually turn the mode on/off in individual tests... – Martin Schmidke Jan 05 '23 at 16:31
  • Are your classes Winform or classes that run asynchronously? Code only runs in order if each process runs to completion. Lots of code runs asynchronously and returns before completion. Lots of people believe that code will run faster Async. That is not always true and can cause more headaches than it is worth. Windows is multi-user and and the Micro can have one or more cores. So a c# app in Windows is already Async without requiring Async methods. For example, you cannot take a serial port and run multiple processes through the port without a way of splitting data back to each process. – jdweng Jan 05 '23 at 17:02

1 Answers1

0

From what I've learned so far the answer is YES.

You CAN count on [ClassInitialize] to not run sooner than necessary. (as opposed to [ClassCleanup] running later)

However, my tests (when I run all) do not jump back and forth between test classes, I can only imagine how it would pan out then. Then you really wouldn't wanna use [ClassCleanup] and [ClassInitialize] in a way they could interfere with each other (e.g. turning on/off special mode).

If your test runner does leapfrog between test classes, I would move sensitive tests to different assemblies and use [AssemblyCleanup] / [AssemblyInitialize].