Skip to content

Commit 88f8ce4

Browse files
[rel/3.8] Fix ClassCleanup not called when the first test in class is ignored (#5071)
Co-authored-by: Youssef1313 <youssefvictor00@gmail.com>
1 parent 8026b02 commit 88f8ce4

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ TestResult DoRun()
617617
// If ClassInitialize method has not been executed, then we should not execute ClassCleanup
618618
// Note that if there is no ClassInitialze method at all, we will still set
619619
// IsClassInitializeExecuted to true in RunClassInitialize
620+
// IsClassInitializeExecuted can be false if all tests in the class are ignored.
620621
|| !IsClassInitializeExecuted)
621622
{
622623
return null;

src/Adapter/MSTest.TestAdapter/Execution/UnitTestRunner.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ internal async Task<TestResult[]> RunSingleTestAsync(TestMethod testMethod, IDic
211211
}
212212
}
213213

214+
ITestContext testContextForClassCleanup = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties, messageLogger, testContextForTestExecution.Context.CurrentTestOutcome);
215+
testMethodInfo?.Parent.RunClassCleanup(testContextForClassCleanup, _classCleanupManager, testMethodInfo, testMethod, result);
216+
214217
if (testMethodInfo?.Parent.Parent.IsAssemblyInitializeExecuted == true)
215218
{
216-
ITestContext testContextForClassCleanup = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties, messageLogger, testContextForTestExecution.Context.CurrentTestOutcome);
217-
testMethodInfo.Parent.RunClassCleanup(testContextForClassCleanup, _classCleanupManager, testMethodInfo, testMethod, result);
218219
ITestContext testContextForAssemblyCleanup = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties, messageLogger, testContextForClassCleanup.Context.CurrentTestOutcome);
219220
RunAssemblyCleanupIfNeeded(testContextForAssemblyCleanup, _classCleanupManager, _typeCache, result);
220221
}

test/IntegrationTests/MSTest.Acceptance.IntegrationTests/IgnoreTests.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ public async Task ClassCleanup_Inheritance_WhenClassIsSkipped()
1818

1919
// Assert
2020
testHostResult.AssertExitCodeIs(ExitCodes.Success);
21-
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 11, skipped: 7);
21+
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 11, skipped: 8);
2222

2323
testHostResult.AssertOutputContains("SubClass.Method");
24+
testHostResult.AssertOutputContains("SubClass.ClassCleanup");
25+
testHostResult.AssertOutputDoesNotContain("SubClass.IgnoredMethod");
2426
}
2527

2628
[TestMethod]
@@ -191,6 +193,11 @@ public class SubClass : IntermediateClass
191193
public static void SubClassCleanup()
192194
=> Console.WriteLine("SubClass.ClassCleanup");
193195
196+
[TestMethod]
197+
[Ignore]
198+
public void IgnoredMethod()
199+
=> Console.WriteLine("SubClass.IgnoredMethod");
200+
194201
[TestMethod]
195202
public void Method()
196203
=> Console.WriteLine("SubClass.Method");

0 commit comments

Comments
 (0)