Skip to content

Commit

Permalink
Fix for microsoft#136. We were not checking if Test Initialize was co…
Browse files Browse the repository at this point in the history
…mplete. Doing so now.
  • Loading branch information
AbhitejJohn committed Apr 9, 2017
1 parent c1b6531 commit 6457fab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Adapter/MSTest.CoreAdapter/Execution/TestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ private TestResult ExecuteInternal(object[] arguments)
var classInstance = this.CreateTestClassInstance(result);
var testContextSetup = false;
bool isExceptionThrown = false;
bool hasTestInitializePassed = false;
Exception testRunnerException = null;

try
Expand All @@ -205,6 +206,7 @@ private TestResult ExecuteInternal(object[] arguments)

if (this.RunTestInitializeMethod(classInstance, result))
{
hasTestInitializePassed = true;
PlatformServiceProvider.Instance.ThreadOperations.ExecuteWithAbortSafety(
() => this.TestMethod.InvokeAsSynchronousTask(classInstance, arguments));
result.Outcome = TestTools.UnitTesting.UnitTestOutcome.Passed;
Expand Down Expand Up @@ -246,7 +248,8 @@ private TestResult ExecuteInternal(object[] arguments)
// if we get here, the test method did not throw the exception
// if the user specified that the test was going to throw an exception, and
// it did not, we should fail the test
if (!isExceptionThrown && this.ExpectedException != null)
// We only perform this check if the test initialize passes and the test method is actually run.
if (hasTestInitializePassed && !isExceptionThrown && this.ExpectedException != null)
{
result.TestFailureException = new TestFailedException(
UnitTestOutcome.Failed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,23 @@ public void TestMethodInfoInvokeShouldSetStackTraceInformationIfTestInitializeTh
" at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.<TestMethodInfoInvokeShouldSetStackTraceInformationIfTestInitializeThrowsUnitTestAssertException>b__");
}

[TestMethodV1]
public void TestMethodInfoInvokeShouldSetTestInitializeExceptionEvenIfMethodHasExpectedExceptionAttriute()
{
// Arrange.
DummyTestClass.TestInitializeMethodBody = classInstance => { UTF.Assert.Fail("dummyFailMessage"); };
this.testClassInfo.TestInitializeMethod = typeof(DummyTestClass).GetMethod("DummyTestInitializeMethod");
const string ErrorMessage = "Assert.Fail failed. dummyFailMessage";
var testMethodInfo = new TestMethodInfo(this.methodInfo, 3600 * 1000, this.testMethodAttribute, this.expectedException, this.testClassInfo, this.testContextImplementation);

// Act.
var exception = testMethodInfo.Invoke(null).TestFailureException as TestFailedException;

// Assert.
Assert.IsNotNull(exception);
Assert.AreEqual(ErrorMessage, exception?.Message);
}

#endregion

#region TestCleanup method setup
Expand Down

0 comments on commit 6457fab

Please # to comment.