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

Today even if classInit wasnt called we used to call class cleanup. f… #372

Merged
merged 9 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal set
}

/// <summary>
/// Gets a value indicating whether is class initialize executed.
/// Gets a value indicating whether class initialize has executed.
/// </summary>
public bool IsClassInitializeExecuted { get; internal set; }

Expand Down Expand Up @@ -306,7 +306,7 @@ public string RunClassCleanup()
return null;
}

lock (this.testClassExecuteSyncObject)
if (this.IsClassInitializeExecuted || this.ClassInitializeMethod == null)
{
try
{
Expand Down Expand Up @@ -340,6 +340,8 @@ public string RunClassCleanup()
StackTraceHelper.GetStackTraceInformation(realException)?.ErrorStackTrace);
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@ public void TestClassInfoClassCleanupMethodSetShouldThrowForMultipleClassCleanup
ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(TypeInspectionException));
}

[TestMethod]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a negative test case. Can we add a positive test case as well?

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()
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -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.<RunClassCleanupShouldReturnAssertFailureExceptionDetails>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down