Skip to content

Commit 189f3b1

Browse files
authored
Today even if classInit wasnt called we used to call class cleanup. f… (#372)
* Today even if classInit wasnt called we used to call class cleanup. fixing that and adding a test to cover the scenario
1 parent 6e89b0c commit 189f3b1

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ internal set
103103
}
104104

105105
/// <summary>
106-
/// Gets a value indicating whether is class initialize executed.
106+
/// Gets a value indicating whether class initialize has executed.
107107
/// </summary>
108108
public bool IsClassInitializeExecuted { get; internal set; }
109109

@@ -306,7 +306,7 @@ public string RunClassCleanup()
306306
return null;
307307
}
308308

309-
lock (this.testClassExecuteSyncObject)
309+
if (this.IsClassInitializeExecuted || this.ClassInitializeMethod == null)
310310
{
311311
try
312312
{
@@ -340,6 +340,8 @@ public string RunClassCleanup()
340340
StackTraceHelper.GetStackTraceInformation(realException)?.ErrorStackTrace);
341341
}
342342
}
343+
344+
return null;
343345
}
344346
}
345347
}

test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,37 @@ public void TestClassInfoClassCleanupMethodSetShouldThrowForMultipleClassCleanup
111111
ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(TypeInspectionException));
112112
}
113113

114+
[TestMethod]
115+
public void TestClassInfoClassCleanupMethodShouldNotInvokeWhenNoTestClassInitializedIsCalled()
116+
{
117+
var classcleanupCallCount = 0;
118+
DummyTestClass.ClassCleanupMethodBody = () => classcleanupCallCount++;
119+
120+
this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod");
121+
this.testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");
122+
123+
var ret = this.testClassInfo.RunClassCleanup(); // call cleanup without calling init
124+
125+
Assert.AreEqual(null, ret);
126+
Assert.AreEqual(0, classcleanupCallCount);
127+
}
128+
129+
[TestMethod]
130+
public void TestClassInfoClassCleanupMethodShouldInvokeWhenTestClassInitializedIsCalled()
131+
{
132+
var classcleanupCallCount = 0;
133+
DummyTestClass.ClassCleanupMethodBody = () => classcleanupCallCount++;
134+
135+
this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod");
136+
this.testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");
137+
138+
this.testClassInfo.RunClassInitialize(this.testContext);
139+
var ret = this.testClassInfo.RunClassCleanup(); // call cleanup without calling init
140+
141+
Assert.AreEqual(null, ret);
142+
Assert.AreEqual(1, classcleanupCallCount);
143+
}
144+
114145
[TestMethod]
115146
public void TestClassInfoHasExecutableCleanupMethodShouldReturnFalseIfClassDoesNotHaveCleanupMethod()
116147
{
@@ -275,6 +306,16 @@ public void RunClassInitializeShouldThrowForAlreadyExecutedTestClassInitWithExce
275306
exception.Message);
276307
}
277308

309+
[TestMethod]
310+
public void RunClassCleanupShouldInvokeIfClassCleanupMethod()
311+
{
312+
var classcleanupCallCount = 0;
313+
DummyTestClass.ClassCleanupMethodBody = () => classcleanupCallCount++;
314+
this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod");
315+
Assert.IsNull(this.testClassInfo.RunClassCleanup());
316+
Assert.AreEqual(1, classcleanupCallCount);
317+
}
318+
278319
[TestMethod]
279320
public void RunAssemblyInitializeShouldPassOnTheTestContextToAssemblyInitMethod()
280321
{
@@ -300,24 +341,13 @@ public void RunClassCleanupShouldNotInvokeIfClassCleanupIsNull()
300341
Assert.AreEqual(0, classcleanupCallCount);
301342
}
302343

303-
[TestMethod]
304-
public void RunClassCleanupShouldInvokeIfClassCleanupMethod()
305-
{
306-
var classcleanupCallCount = 0;
307-
DummyTestClass.ClassCleanupMethodBody = () => classcleanupCallCount++;
308-
309-
this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod");
310-
311-
Assert.IsNull(this.testClassInfo.RunClassCleanup());
312-
Assert.AreEqual(1, classcleanupCallCount);
313-
}
314-
315344
[TestMethod]
316345
public void RunClassCleanupShouldReturnAssertFailureExceptionDetails()
317346
{
318347
DummyTestClass.ClassCleanupMethodBody = () => UTF.Assert.Fail("Test Failure.");
319348

320349
this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod("ClassCleanupMethod");
350+
321351
StringAssert.StartsWith(
322352
this.testClassInfo.RunClassCleanup(),
323353
"Class Cleanup method DummyTestClass.ClassCleanupMethod failed. Error Message: Assert.Fail failed. Test Failure.. Stack Trace: at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.<RunClassCleanupShouldReturnAssertFailureExceptionDetails>");

test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/UnitTestRunnerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution
1414
using System.Reflection;
1515
using System.Text;
1616
using System.Xml;
17-
1817
using global::MSTestAdapter.TestUtilities;
1918
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
2019
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;

0 commit comments

Comments
 (0)