Skip to content

Commit 8469276

Browse files
authored
Fix for #136. We were not checking if Test Initialize was complete. Doing so now. (#146)
1 parent c1b6531 commit 8469276

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ private TestResult ExecuteInternal(object[] arguments)
192192
var classInstance = this.CreateTestClassInstance(result);
193193
var testContextSetup = false;
194194
bool isExceptionThrown = false;
195+
bool hasTestInitializePassed = false;
195196
Exception testRunnerException = null;
196197

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

206207
if (this.RunTestInitializeMethod(classInstance, result))
207208
{
209+
hasTestInitializePassed = true;
208210
PlatformServiceProvider.Instance.ThreadOperations.ExecuteWithAbortSafety(
209211
() => this.TestMethod.InvokeAsSynchronousTask(classInstance, arguments));
210212
result.Outcome = TestTools.UnitTesting.UnitTestOutcome.Passed;
@@ -246,7 +248,8 @@ private TestResult ExecuteInternal(object[] arguments)
246248
// if we get here, the test method did not throw the exception
247249
// if the user specified that the test was going to throw an exception, and
248250
// it did not, we should fail the test
249-
if (!isExceptionThrown && this.ExpectedException != null)
251+
// We only perform this check if the test initialize passes and the test method is actually run.
252+
if (hasTestInitializePassed && !isExceptionThrown && this.ExpectedException != null)
250253
{
251254
result.TestFailureException = new TestFailedException(
252255
UnitTestOutcome.Failed,

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

+17
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,23 @@ public void TestMethodInfoInvokeShouldSetStackTraceInformationIfTestInitializeTh
430430
" at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.<TestMethodInfoInvokeShouldSetStackTraceInformationIfTestInitializeThrowsUnitTestAssertException>b__");
431431
}
432432

433+
[TestMethodV1]
434+
public void TestMethodInfoInvokeShouldSetTestInitializeExceptionEvenIfMethodHasExpectedExceptionAttriute()
435+
{
436+
// Arrange.
437+
DummyTestClass.TestInitializeMethodBody = classInstance => { UTF.Assert.Fail("dummyFailMessage"); };
438+
this.testClassInfo.TestInitializeMethod = typeof(DummyTestClass).GetMethod("DummyTestInitializeMethod");
439+
const string ErrorMessage = "Assert.Fail failed. dummyFailMessage";
440+
var testMethodInfo = new TestMethodInfo(this.methodInfo, 3600 * 1000, this.testMethodAttribute, this.expectedException, this.testClassInfo, this.testContextImplementation);
441+
442+
// Act.
443+
var exception = testMethodInfo.Invoke(null).TestFailureException as TestFailedException;
444+
445+
// Assert.
446+
Assert.IsNotNull(exception);
447+
Assert.AreEqual(ErrorMessage, exception?.Message);
448+
}
449+
433450
#endregion
434451

435452
#region TestCleanup method setup

0 commit comments

Comments
 (0)