Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCleanup should trigger after all test methods have been triggered #483

Closed
millsy013 opened this issue Sep 5, 2018 · 2 comments
Closed

Comments

@millsy013
Copy link

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.

@vlaskal
Copy link

vlaskal commented Sep 19, 2019

Hi, I think it is same issue as is #580

@Evangelink
Copy link
Member

Hey folks. This is a super confusing behavior of MSTest! Basically ClassCleanup default mode is equivalent to AssemblyCleanup and is run only after the last test of the run. There is a property you can use to change this [ClassCleanup(ClassCleanupBehavior.EndOfClass)].

Note that we are planning to change the default in the next major release of MSTest (after v3), see #1316

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants