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

Change ClassCleanup default behavior to be "EndOfClass" #1538

Merged
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void RunClassInitialize(TestContext testContext)
/// <returns>
/// Any exception that can be thrown as part of a class cleanup as warning messages.
/// </returns>
public string? RunClassCleanup(ClassCleanupBehavior classCleanupLifecycle = ClassCleanupBehavior.EndOfAssembly)
public string? RunClassCleanup(ClassCleanupBehavior classCleanupLifecycle = ClassCleanupBehavior.EndOfClass)
{
if (ClassCleanupMethod is null && BaseClassInitAndCleanupMethods.All(p => p.Item2 == null))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/Execution/UnitTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal void InitializeClassCleanupManager(ICollection<UnitTestElement> testsTo
{
// We can't transport the Enum across AppDomain boundaries because of backwards and forwards compatibility.
// So we're converting here if we can, or falling back to the default.
var lifecycle = ClassCleanupBehavior.EndOfAssembly;
var lifecycle = ClassCleanupBehavior.EndOfClass;
if (classCleanupLifecycle != null && Enum.IsDefined(typeof(ClassCleanupBehavior), classCleanupLifecycle))
{
lifecycle = (ClassCleanupBehavior)classCleanupLifecycle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.H
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.IsClassCleanupExecuted.get -> bool
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.IsClassInitializeExecuted.get -> bool
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.Parent.get -> Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestAssemblyInfo!
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.RunClassCleanup(Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior classCleanupLifecycle = Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior.EndOfAssembly) -> string?
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.RunClassCleanup(Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior classCleanupLifecycle = Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior.EndOfClass) -> string?
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.RunClassInitialize(Microsoft.VisualStudio.TestTools.UnitTesting.TestContext! testContext) -> void
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.TestCleanupMethod.get -> System.Reflection.MethodInfo?
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestClassInfo.TestContextProperty.get -> System.Reflection.PropertyInfo?
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#nullable enable
#nullable enable
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;
public enum ClassCleanupBehavior
{
/// <summary>
/// Run at end of assembly.
/// Run at end of class.
/// </summary>
EndOfAssembly,
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
EndOfClass,

/// <summary>
/// Run at end of class.
/// Run at end of assembly.
/// </summary>
EndOfClass,
EndOfAssembly,
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = true)]
public class ClassCleanupExecutionAttribute : Attribute
{
/// <summary>
/// Default class cleanup execution.
/// </summary>
public static readonly ClassCleanupBehavior DefaultClassCleanupLifecycle = ClassCleanupBehavior.EndOfAssembly;

/// <summary>
/// Initializes a new instance of the <see cref="ClassCleanupExecutionAttribute"/> class.
/// </summary>
public ClassCleanupExecutionAttribute()
: this(DefaultClassCleanupLifecycle)
: this(ClassCleanupBehavior.EndOfClass)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute.ClassCleanupA
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute.CleanupBehavior.get -> Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior?
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute.InheritanceBehavior.get -> Microsoft.VisualStudio.TestTools.UnitTesting.InheritanceBehavior
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior.EndOfAssembly = 0 -> Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior.EndOfClass = 1 -> Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior.EndOfAssembly = 1 -> Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior.EndOfClass = 0 -> Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupExecutionAttribute
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupExecutionAttribute.ClassCleanupExecutionAttribute() -> void
Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupExecutionAttribute.ClassCleanupExecutionAttribute(Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior cleanupBehavior) -> void
Expand Down Expand Up @@ -326,7 +326,6 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.That.get ->
static Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.LogMessage(string! format, params object?[]! args) -> void
static Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.OnLogMessage -> Microsoft.VisualStudio.TestTools.UnitTesting.Logging.Logger.LogMessageHandler?
static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.That.get -> Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert!
static readonly Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupExecutionAttribute.DefaultClassCleanupLifecycle -> Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupBehavior
static readonly Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute.DefaultDataAccessMethod -> Microsoft.VisualStudio.TestTools.UnitTesting.DataAccessMethod
static readonly Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute.DefaultProviderName -> string!
virtual Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute.GetDisplayName(System.Reflection.MethodInfo! methodInfo, object?[]? data) -> string?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private void ValidateTestRunLifecycle(string targetFramework)
targetFramework: targetFramework);
RunEventsHandler.PassedTests.Should().HaveCount(27); // The inherit class tests are called twice.

// The cleanup will appear in this case as the default ClassCleanup is EndOfClass.
var caseClassCleanup = RunEventsHandler.PassedTests.Single(x => x.TestCase.FullyQualifiedName.Contains("LifeCycleClassCleanup.TestMethod"));
caseClassCleanup.Outcome.Should().Be(Microsoft.VisualStudio.TestPlatform.ObjectModel.TestOutcome.Passed);
caseClassCleanup.Messages.Should().HaveCount(3);
Expand All @@ -167,6 +168,7 @@ private void ValidateTestRunLifecycle(string targetFramework)
{(targetFramework == "net6.0"
? "Console: LifeCycleClassCleanup.DisposeAsync was called\r\nConsole: LifeCycleClassCleanup.Dispose was called"
: "Console: LifeCycleClassCleanup.Dispose was called")}
Console: LifeCycleClassCleanup.ClassCleanup was called
Evangelink marked this conversation as resolved.
Show resolved Hide resolved

""");
caseClassCleanup.Messages[1].Text.Should().Be(
Expand All @@ -185,7 +187,8 @@ private void ValidateTestRunLifecycle(string targetFramework)
+ "\r\n"
+ GenerateTraceDebugPrefixedMessage("LifeCycleClassCleanup.Dispose was called")
: GenerateTraceDebugPrefixedMessage("LifeCycleClassCleanup.Dispose was called"))}

{GenerateTraceDebugPrefixedMessage("LifeCycleClassCleanup.ClassCleanup was called")}

""");
caseClassCleanup.Messages[2].Text.Should().Be(
$"""
Expand All @@ -201,6 +204,7 @@ LifeCycleClassCleanup.TestCleanup was called
{(targetFramework == "net6.0"
? "LifeCycleClassCleanup.DisposeAsync was called\r\nLifeCycleClassCleanup.Dispose was called"
: "LifeCycleClassCleanup.Dispose was called")}
LifeCycleClassCleanup.ClassCleanup was called

""");

Expand Down Expand Up @@ -1568,7 +1572,6 @@ LifeCycleClassCleanupEndOfClassAndNone.TestCleanup was called
var expectedRemainingMessages =
"""
Console: LifeCycleDerivedClassInitializeNoneAndClassCleanupBeforeEachDerivedClass.ClassCleanup was called
Console: LifeCycleClassCleanup.ClassCleanup was called
Console: LifeCycleClassCleanupEndOfAssemblyAndBeforeEachDerivedClass.ClassCleanup was called
Console: LifeCycleClassInitializeNoneAndClassCleanupBeforeEachDerivedClass.ClassCleanup was called
Console: LifeCycleDerivedClassCleanupEndOfAssemblyAndNone.ClassCleanup was called
Expand Down Expand Up @@ -1624,7 +1627,6 @@ LifeCycleClassCleanupEndOfClassAndNone.TestCleanup was called
{GenerateTraceDebugPrefixedMessage("LifeCycleClassInitializeAndCleanupNone.ClassCleanup was called")}
{GenerateTraceDebugPrefixedMessage("LifeCycleClassCleanupEndOfAssemblyAndNone.ClassCleanup was called")}
{GenerateTraceDebugPrefixedMessage("LifeCycleDerivedClassInitializeBeforeEachDerivedClassAndClassCleanupNone.ClassCleanup was called")}
{GenerateTraceDebugPrefixedMessage("LifeCycleClassCleanup.ClassCleanup was called")}
{GenerateTraceDebugPrefixedMessage("LifeCycleDerivedClassInitializeNoneAndClassCleanupBeforeEachDerivedClass.ClassCleanup was called")}
{GenerateTraceDebugPrefixedMessage("LifeCycleClassInitializeBeforeEachDerivedClassAndClassCleanupNone.ClassCleanup was called")}
{GenerateTraceDebugPrefixedMessage("LifeCycleDerivedClassInitializeAndCleanupNone.ClassCleanup was called")}
Expand Down Expand Up @@ -1676,7 +1678,6 @@ LifeCycleDerivedClassCleanupEndOfClassAndBeforeEachDerivedClass.ClassCleanup was
LifeCycleDerivedClassCleanupEndOfAssemblyAndBeforeEachDerivedClass.ClassCleanup was called
LifeCycleDerivedClassCleanupEndOfClassAndNone.ClassCleanup was called
LifeCycleClassCleanupEndOfAssemblyAndNone.ClassCleanup was called
LifeCycleClassCleanup.ClassCleanup was called
LifeCycleClassInitializeNoneAndClassCleanupBeforeEachDerivedClass.ClassCleanup was called
LifeCycleDerivedClassInitializeAndCleanupNone.ClassCleanup was called
LifeCycleClassInitializeAndCleanupNone.ClassCleanup was called
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ValueTask DisposeAsync()
}
#endif

[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)]
[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass, ClassCleanupBehavior.EndOfAssembly)]
public static void ClassCleanup()
{
s_testContext.WriteLine("LifeCycleClassInitializeAndCleanupBeforeEachDerivedClass.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ValueTask DisposeAsync()
}
#endif

[ClassCleanup(InheritanceBehavior.None)]
[ClassCleanup(InheritanceBehavior.None, ClassCleanupBehavior.EndOfAssembly)]
public static void ClassCleanup()
{
s_testContext.WriteLine("LifeCycleClassInitializeAndCleanupNone.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ValueTask DisposeAsync()
}
#endif

[ClassCleanup(InheritanceBehavior.None)]
[ClassCleanup(InheritanceBehavior.None, ClassCleanupBehavior.EndOfAssembly)]
public static void ClassCleanup()
{
s_testContext.WriteLine("LifeCycleClassInitializeBeforeEachDerivedClassAndClassCleanupNone.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ValueTask DisposeAsync()
}
#endif

[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)]
[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass, ClassCleanupBehavior.EndOfAssembly)]
public static void ClassCleanup()
{
s_testContext.WriteLine("LifeCycleClassInitializeNoneAndClassCleanupBeforeEachDerivedClass.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void DerivedClassTestCleanup()
Debug.WriteLine("Debug: LifeCycleDerivedClassCleanupEndOfAssemblyAndBeforeEachDerivedClass.TestCleanup was called");
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
public static void DerivedClassCleanup()
{
s_testContext.WriteLine("LifeCycleDerivedClassCleanupEndOfAssemblyAndBeforeEachDerivedClass.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void DerivedClassTestCleanup()
Debug.WriteLine("Debug: LifeCycleDerivedClassCleanupEndOfAssemblyAndNone.TestCleanup was called");
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
public static void DerivedClassCleanup()
{
s_testContext.WriteLine("LifeCycleDerivedClassCleanupEndOfAssemblyAndNone.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void DerivedClassTestCleanup()
Debug.WriteLine("Debug: LifeCycleDerivedClassCleanupEndOfClassAndBeforeEachDerivedClass.TestCleanup was called");
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
public static void DerivedClassCleanup()
{
s_testContext.WriteLine("LifeCycleDerivedClassCleanupEndOfClassAndBeforeEachDerivedClass.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void DerivedClassTestCleanup()
Debug.WriteLine("Debug: LifeCycleDerivedClassCleanupEndOfClassAndNone.TestCleanup was called");
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
public static void DerivedClassCleanup()
{
s_testContext.WriteLine("LifeCycleDerivedClassCleanupEndOfClassAndNone.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void DerivedClassTestCleanup()
Debug.WriteLine("Debug: LifeCycleDerivedClassInitializeAndCleanupBeforeEachDerivedClass.TestCleanup was called");
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
public static void DerivedClassCleanup()
{
s_testContext.WriteLine("LifeCycleDerivedClassInitializeAndCleanupBeforeEachDerivedClass.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void DerivedClassTestCleanup()
Debug.WriteLine("Debug: LifeCycleDerivedClassInitializeAndCleanupNone.TestCleanup was called");
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
public static void DerivedClassCleanup()
{
s_testContext.WriteLine("LifeCycleDerivedClassInitializeAndCleanupNone.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void DerivedClassTestCleanup()
Debug.WriteLine("Debug: LifeCycleDerivedClassInitializeBeforeEachDerivedClassAndClassCleanupNone.TestCleanup was called");
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
public static void DerivedClassCleanup()
{
s_testContext.WriteLine("LifeCycleDerivedClassInitializeBeforeEachDerivedClassAndClassCleanupNone.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void DerivedClassTestCleanup()
Debug.WriteLine("Debug: LifeCycleDerivedClassInitializeNoneAndClassCleanupBeforeEachDerivedClass.TestCleanup was called");
}

[ClassCleanup]
[ClassCleanup(ClassCleanupBehavior.EndOfAssembly)]
public static void DerivedClassCleanup()
{
s_testContext.WriteLine("LifeCycleDerivedClassInitializeNoneAndClassCleanupBeforeEachDerivedClass.ClassCleanup was called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public void RunClassCleanupShouldReturnAssertFailureExceptionDetails()
_testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.ClassCleanupMethod));

// Act
var classCleanup = _testClassInfo.RunClassCleanup();
var classCleanup = _testClassInfo.RunClassCleanup(UTF.ClassCleanupBehavior.EndOfAssembly);
Evangelink marked this conversation as resolved.
Show resolved Hide resolved

// Assert
Verify(classCleanup.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed."));
Expand All @@ -506,7 +506,7 @@ public void RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails()
_testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.ClassCleanupMethod));

// Act
var classCleanup = _testClassInfo.RunClassCleanup();
var classCleanup = _testClassInfo.RunClassCleanup(UTF.ClassCleanupBehavior.EndOfAssembly);

// Assert
Verify(classCleanup.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed."));
Expand All @@ -521,7 +521,7 @@ public void RunClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions()
_testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.ClassCleanupMethod));

// Act
var classCleanup = _testClassInfo.RunClassCleanup();
var classCleanup = _testClassInfo.RunClassCleanup(UTF.ClassCleanupBehavior.EndOfAssembly);

// Assert
Verify(classCleanup.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed."));
Expand All @@ -539,7 +539,7 @@ public void RunBaseClassCleanupWithNoDerivedClassCleanupShouldReturnExceptionDet
_testClassInfo.BaseClassCleanupMethodsStack.Push(baseClassCleanupMethod);

// Act
var classCleanup = _testClassInfo.RunClassCleanup();
var classCleanup = _testClassInfo.RunClassCleanup(UTF.ClassCleanupBehavior.EndOfAssembly);

// Assert
Verify(classCleanup.StartsWith("Class Cleanup method DummyBaseTestClass.CleanupClassMethod failed."));
Expand Down