What is the life cycle of unit tests in C #

What is the sequence of events in a full MSTest run of unit tests in C # inside Visual Studio (Ctrl + R, A)?

Here is what I think so far:

  • 1 - Executes [AssemblyInitialize]
  • 2 - Randomly runs a [ClassInitialize]
  • 3 - Run the class [TestInitialize]
  • 4 - Randomly runs a [TestMethod] from this class
  • 5 - runs the class [TestCleanup]
  • Repeat 3 through 5 for each TestMethod in the class.
  • Repeat 2-5 for each test class.
  • 6 - runs all classes [ClassCleanup] methods
  • 7 - Executes [AssemblyCleanup]

But I think that VS can initialize several classes at once, and then arbitrarily run TestMethods. Should tests be standalone throughout the class or throughout the test project or even the entire solution? Knowing the exact sequence of events should answer these questions.

UPDATE:

I did some tests and found that this is really the order in which events occur, with the exception of # 3 - 5, where ANY test from ANY class can be run. Visual Studio seems to run one test at a time in sequence. However, one should not rely on this for the reasons stated in the accepted answer.

+5
source share
2 answers

You're right. This is really the order in which the code will be executed. However, since the tests must be completely independent, there is no guarantee that they will be executed in order and that they will be executed in the same thread. A frame can run multiple tests at once.

You can also force the execution of a specific test order using test cases, but this is considered bad practice, as test cases should be used to re-build tests (instead of tags).

+1
source

To determine the specific order for your tests, either create an ordered test ( http://msdn.microsoft.com/en-us/library/ms182631.aspx ), or create a mapping file and call mstest. exe for each test case in the order you specify ( http://msdn.microsoft.com/en-us/library/ms182489(VS.80).aspx )

+1
source

Source: https://habr.com/ru/post/1200754/


All Articles