Skip to content

Commit

Permalink
[rel/3.8] Fix ClassCleanup not called when the first test in class is…
Browse files Browse the repository at this point in the history
… ignored (#5071)

Co-authored-by: Youssef1313 <youssefvictor00@gmail.com>
  • Loading branch information
youssef-backport-bot and Youssef1313 authored Feb 19, 2025
1 parent 8026b02 commit 88f8ce4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ TestResult DoRun()
// If ClassInitialize method has not been executed, then we should not execute ClassCleanup
// Note that if there is no ClassInitialze method at all, we will still set
// IsClassInitializeExecuted to true in RunClassInitialize
// IsClassInitializeExecuted can be false if all tests in the class are ignored.
|| !IsClassInitializeExecuted)
{
return null;
Expand Down
5 changes: 3 additions & 2 deletions src/Adapter/MSTest.TestAdapter/Execution/UnitTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@ internal async Task<TestResult[]> RunSingleTestAsync(TestMethod testMethod, IDic
}
}

ITestContext testContextForClassCleanup = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties, messageLogger, testContextForTestExecution.Context.CurrentTestOutcome);
testMethodInfo?.Parent.RunClassCleanup(testContextForClassCleanup, _classCleanupManager, testMethodInfo, testMethod, result);

if (testMethodInfo?.Parent.Parent.IsAssemblyInitializeExecuted == true)
{
ITestContext testContextForClassCleanup = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties, messageLogger, testContextForTestExecution.Context.CurrentTestOutcome);
testMethodInfo.Parent.RunClassCleanup(testContextForClassCleanup, _classCleanupManager, testMethodInfo, testMethod, result);
ITestContext testContextForAssemblyCleanup = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties, messageLogger, testContextForClassCleanup.Context.CurrentTestOutcome);
RunAssemblyCleanupIfNeeded(testContextForAssemblyCleanup, _classCleanupManager, _typeCache, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public async Task ClassCleanup_Inheritance_WhenClassIsSkipped()

// Assert
testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 11, skipped: 7);
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 11, skipped: 8);

testHostResult.AssertOutputContains("SubClass.Method");
testHostResult.AssertOutputContains("SubClass.ClassCleanup");
testHostResult.AssertOutputDoesNotContain("SubClass.IgnoredMethod");
}

[TestMethod]
Expand Down Expand Up @@ -191,6 +193,11 @@ public class SubClass : IntermediateClass
public static void SubClassCleanup()
=> Console.WriteLine("SubClass.ClassCleanup");
[TestMethod]
[Ignore]
public void IgnoredMethod()
=> Console.WriteLine("SubClass.IgnoredMethod");
[TestMethod]
public void Method()
=> Console.WriteLine("SubClass.Method");
Expand Down

0 comments on commit 88f8ce4

Please # to comment.