/// <summary> /// Usually this method shouldn't be invoked directly. Use <see cref="TestClass.Run"/> instead. If no test class has /// been specified in the constructor, this method only executes the method itself (but no class setup or teardown /// methods). /// </summary> public void Run(object testClassInstance, int testMethodIndex, ITestResultHandler resultHandler) { this.State = TestState.Running; resultHandler.OnTestMethodStarted(this, testMethodIndex); Exception outcomeError = null; if (this.Class.SetupMethod != null) { outcomeError = InvokeTestMethod(testClassInstance, this.Class.SetupMethod); } if (outcomeError == null) { // No error in the setup method outcomeError = InvokeTestMethod(testClassInstance, this.Method); if (outcomeError != null && IsExpectedException(this.Method, outcomeError)) { outcomeError = null; } if (this.Class.TeardownMethod != null) { var e = InvokeTestMethod(testClassInstance, this.Class.TeardownMethod); // Make sure we don't override the actual failure reson. if (e != null && outcomeError == null) { outcomeError = e; } } } SetOutcome(outcomeError); resultHandler.OnTestMethodEnded(this, testMethodIndex); }
public void RunTests(ITestResultHandler resultHandler) { lock (this) { if (this.m_isRunning) { throw new InvalidOperationException("Tests are currently running."); } this.m_isRunning = true; // Reset all test classes foreach (var item in this.m_testClasses) { item.Value.Reset(); } resultHandler.OnTestRunStarted(this); int index = 1; foreach (var item in this.m_testClasses) { item.Value.Run(resultHandler, index); index++; } resultHandler.OnTestRunEnded(this); this.m_isRunning = false; } }
internal void Run(ITestResultHandler resultHandler, int testClassIndex) { this.State = TestState.Running; resultHandler.OnTestClassTestStarted(this, testClassIndex); object testClassInstance; try { testClassInstance = Activator.CreateInstance(this.Class); } catch (Exception e) { if (e is TargetInvocationException && e.InnerException != null) { e = e.InnerException; } this.State = TestState.Failed; this.TestClassError = e; this.ErrorType = TestClassErrorType.ConstructorError; resultHandler.OnTestClassError(this, testClassIndex); return; } if (this.ClassSetupMethod != null) { var e = TestMethod.InvokeTestMethod(testClassInstance, this.ClassSetupMethod); if (e != null) { this.State = TestState.Failed; this.TestClassError = e; this.ErrorType = TestClassErrorType.ClassInitializerError; resultHandler.OnTestClassError(this, testClassIndex); return; } } int testMethodIndex = 1; this.m_stateCount[TestState.Running] = 1; foreach (TestMethod testMethod in this.TestMethods) { testMethod.Run(testClassInstance, testMethodIndex, resultHandler); this.m_stateCount[testMethod.State] = this.m_stateCount[testMethod.State] + 1; this.m_stateCount[TestState.NotYetRun] = this.m_stateCount[TestState.NotYetRun] - 1; testMethodIndex++; } this.m_stateCount[TestState.Running] = 0; if (this.ClassTeardownMethod != null) { var e = TestMethod.InvokeTestMethod(testClassInstance, this.ClassTeardownMethod); if (e != null) { this.State = TestState.Failed; this.TestClassError = e; this.ErrorType = TestClassErrorType.ClassCleanupError; resultHandler.OnTestClassError(this, testClassIndex); return; } } if (GetStateCount(TestState.Failed) != 0) { this.State = TestState.Failed; } else if (GetStateCount(TestState.Inconclusive) != 0) { this.State = TestState.Inconclusive; } else { this.State = TestState.Passed; } resultHandler.OnTestClassTestEnded(this, testClassIndex); }