Improvement: ClassCleanup method to trigger after worker has executed all test methods not after all workers are complete
See original GitHub issueWhen 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:
- UnitTest2.ClassInit()
- UnitTest2.TestMethod()
- UnitTest1.ClassInit()
- UnitTest1.TestMethod()
- UnitTest2.ClassCleanup()
- 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:
- UnitTest2.ClassInit()
- UnitTest2.TestMethod()
- UnitTest2.ClassCleanup() - Expected
- UnitTest1.ClassInit()
- UnitTest1.TestMethod()
- 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:
- Created 5 years ago
- Reactions:11
- Comments:30 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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).
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.