question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improvement: ClassCleanup method to trigger after worker has executed all test methods not after all workers are complete

See original GitHub issue

When running multiple [TestClass]es at the same time, why does the [ClassCleanup()] methods only get called at the very end?

File: UnitTest1.cs

    [TestClass]
    public class UnitTest1
    {
        [ClassInitialize()]
        public static void ClassInit(TestContext context) { }
    
        [ClassCleanup()]
        public static void ClassCleanup() { }
    
        [TestMethod]
        public void TestMethod() { }
    }

File: UnitTest2.cs

    [TestClass]
    public class UnitTest2
    {
        [ClassInitialize()]
        public static void ClassInit(TestContext context) { }
    
        [ClassCleanup()]
        public static void ClassCleanup() { }
    
        [TestMethod]
        public void TestMethod() { }
    }

The execution order is the following:

  1. UnitTest2.ClassInit()
  2. UnitTest2.TestMethod()
  3. UnitTest1.ClassInit()
  4. UnitTest1.TestMethod()
  5. UnitTest2.ClassCleanup()
  6. UnitTest1.ClassCleanup()

Notice that both [ClassCleanup] methods are executed at the end of the set; not after each TestClass is “done”.

But I expected a different behavior:

  1. UnitTest2.ClassInit()
  2. UnitTest2.TestMethod()
  3. UnitTest2.ClassCleanup() - Expected
  4. UnitTest1.ClassInit()
  5. UnitTest1.TestMethod()
  6. UnitTest1.ClassCleanup() - Expected

Microsoft’s Documentation - ClassCleanupAttribute Class says, “Identifies a method that contains code to be used after ALL the tests in the test class have run and to free resources obtained by the test class.”

But it appears to be run/executed late, after ALL tests have run. This seems wrong, and it is blocking my development.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:30 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
ScottMichaudcommented, Feb 3, 2020

Assembly and class cleanup today run at the end. Could you please elaborate on why this would block your development ?

It looks like the thread has already made some valid use cases, but here’s another example: Using Appium.WebDriver to run integration tests on a single-instance application with non-trivial load and close.

I would expect that each TestClass could be used to launch the application into a specific state, run the unit tests on that state, then close the application. Because the ClassCleanup doesn’t run before the next class attempts to start testing, the driver attempts to launch the application a second time in a different mode while the first instance is still open.

We’re currently using TestInitialize and TestCleanup to allow a “Run All” to succeed, but this significantly limits our ability to break apart our integration tests into separate, well defined TestMethods due to how much execution time it adds.

Of course, initialization and clean-up at arbitrary levels of granularity (ex: groups) could help minimize execution time further, by batching tests to minimize state changes within an application run, but TestClasses would cover most of that need (by cutting out the load and shutdown cycle).

3reactions
Haploiscommented, Nov 23, 2021

This is fixed in https://github.com/microsoft/testfx/pull/968 and released with 2.2.8. To enable it you can set ClassCleanupBehavior to ClassCleanupBehavior.EndOfClass in your ClassCleanup attribute.

This can also be set on assembly level by ClassCleanupExecution assembly level attribute.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mstest [ClassCleanup] not executed at all in VS 2019
Problem is, that this method is executed after all of your tests are completed, so you cannot write message from that method in...
Read more >
ClassCleanup attribute not working as expected
This is by design as of today. There is no guarantee that the class cleanup will run immediately after all tests from that...
Read more >
TestInitialize - somewhat abstract
In this post, we will look at how we can share setup and cleanup code across tests in a test class in XUnit....
Read more >
Most Complete MSTest Framework Tutorial Using .Net Core
Methods that will be called only once before executing any of the test methods present in that class. [ClassCleanup], Methods that will be ......
Read more >
ClassCleanupAttribute Class
Identifies a method that contains code to be used after all the tests in the test class have run and to free resources...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found