diff --git a/src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs b/src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs index cc27d5daa8..568d022043 100644 --- a/src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs +++ b/src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs @@ -103,7 +103,7 @@ internal set } /// - /// Gets a value indicating whether is class initialize executed. + /// Gets a value indicating whether class initialize has executed. /// public bool IsClassInitializeExecuted { get; internal set; } @@ -306,7 +306,7 @@ public string RunClassCleanup() return null; } - lock (this.testClassExecuteSyncObject) + if (this.IsClassInitializeExecuted || this.ClassInitializeMethod == null) { try { @@ -340,6 +340,8 @@ public string RunClassCleanup() StackTraceHelper.GetStackTraceInformation(realException)?.ErrorStackTrace); } } + + return null; } } } \ No newline at end of file diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs index bb4e78c111..e3f8285b88 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs @@ -111,6 +111,37 @@ public void TestClassInfoClassCleanupMethodSetShouldThrowForMultipleClassCleanup ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(TypeInspectionException)); } + [TestMethod] + public void TestClassInfoClassCleanupMethodShouldNotInvokeWhenNoTestClassInitializedIsCalled() + { + var classcleanupCallCount = 0; + DummyTestClass.ClassCleanupMethodBody = () => classcleanupCallCount++; + + this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod"); + this.testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod"); + + var ret = this.testClassInfo.RunClassCleanup(); // call cleanup without calling init + + Assert.AreEqual(null, ret); + Assert.AreEqual(0, classcleanupCallCount); + } + + [TestMethod] + public void TestClassInfoClassCleanupMethodShouldInvokeWhenTestClassInitializedIsCalled() + { + var classcleanupCallCount = 0; + DummyTestClass.ClassCleanupMethodBody = () => classcleanupCallCount++; + + this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod"); + this.testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod"); + + this.testClassInfo.RunClassInitialize(this.testContext); + var ret = this.testClassInfo.RunClassCleanup(); // call cleanup without calling init + + Assert.AreEqual(null, ret); + Assert.AreEqual(1, classcleanupCallCount); + } + [TestMethod] public void TestClassInfoHasExecutableCleanupMethodShouldReturnFalseIfClassDoesNotHaveCleanupMethod() { @@ -275,6 +306,16 @@ public void RunClassInitializeShouldThrowForAlreadyExecutedTestClassInitWithExce exception.Message); } + [TestMethod] + public void RunClassCleanupShouldInvokeIfClassCleanupMethod() + { + var classcleanupCallCount = 0; + DummyTestClass.ClassCleanupMethodBody = () => classcleanupCallCount++; + this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod"); + Assert.IsNull(this.testClassInfo.RunClassCleanup()); + Assert.AreEqual(1, classcleanupCallCount); + } + [TestMethod] public void RunAssemblyInitializeShouldPassOnTheTestContextToAssemblyInitMethod() { @@ -300,24 +341,13 @@ public void RunClassCleanupShouldNotInvokeIfClassCleanupIsNull() Assert.AreEqual(0, classcleanupCallCount); } - [TestMethod] - public void RunClassCleanupShouldInvokeIfClassCleanupMethod() - { - var classcleanupCallCount = 0; - DummyTestClass.ClassCleanupMethodBody = () => classcleanupCallCount++; - - this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod"); - - Assert.IsNull(this.testClassInfo.RunClassCleanup()); - Assert.AreEqual(1, classcleanupCallCount); - } - [TestMethod] public void RunClassCleanupShouldReturnAssertFailureExceptionDetails() { DummyTestClass.ClassCleanupMethodBody = () => UTF.Assert.Fail("Test Failure."); this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod"); + StringAssert.StartsWith( this.testClassInfo.RunClassCleanup(), "Class Cleanup method DummyTestClass.ClassCleanupMethod failed. Error Message: Assert.Fail failed. Test Failure.. Stack Trace: at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c."); diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/UnitTestRunnerTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/UnitTestRunnerTests.cs index 9644cd927a..0c47758f87 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/UnitTestRunnerTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/UnitTestRunnerTests.cs @@ -14,7 +14,6 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution using System.Reflection; using System.Text; using System.Xml; - using global::MSTestAdapter.TestUtilities; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;