Example #1
0
        internal TestMethodInfo(
            MethodInfo testMethod,
            TestClassInfo parent,
            TestMethodOptions testmethodOptions,
            TestExecutionRecorderWrapper testExecutionRecorder)
        {
            Debug.Assert(testMethod != null, "TestMethod should not be null");
            Debug.Assert(parent != null, "Parent should not be null");
            Debug.Assert(testExecutionRecorder != null, "TestExecutionRecorder should not be null");

            this.TestMethod            = testMethod;
            this.Parent                = parent;
            this.TestMethodOptions     = testmethodOptions;
            this.TestExecutionRecorder = testExecutionRecorder;
        }
Example #2
0
        /// <summary>
        /// Runs a single test.
        /// </summary>
        /// <param name="testMethod"> The test Method. </param>
        /// <param name="testExecutionRecorder">A instance of TestExecutionRecorderWrapper used to log test execution.</param>
        /// <param name="testContextProperties"> The test context properties. </param>
        /// <returns> The <see cref="UnitTestResult"/>. </returns>
        internal UnitTestResult[] RunSingleTest(TestMethod testMethod, TestExecutionRecorderWrapper testExecutionRecorder, IDictionary <string, object> testContextProperties)
        {
            if (testMethod == null)
            {
                throw new ArgumentNullException("testMethod");
            }

            if (testExecutionRecorder == null)
            {
                throw new ArgumentNullException(nameof(testExecutionRecorder));
            }

            try
            {
                using (var writer = new ThreadSafeStringWriter(CultureInfo.InvariantCulture))
                {
                    var properties  = new Dictionary <string, object>(testContextProperties);
                    var testContext = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, properties);
                    testContext.SetOutcome(TestTools.UnitTesting.UnitTestOutcome.InProgress);

                    // Get the testMethod
                    var testMethodInfo = this.typeCache.GetTestMethodInfo(testMethod, testContext, testExecutionRecorder, MSTestSettings.CurrentSettings.CaptureDebugTraces);

                    // If the specified TestMethod could not be found, return a NotFound result.
                    if (testMethodInfo == null)
                    {
                        return(new UnitTestResult[] { new UnitTestResult(UnitTestOutcome.NotFound, string.Format(CultureInfo.CurrentCulture, Resource.TestNotFound, testMethod.Name)) });
                    }

                    // If test cannot be executed, then bail out.
                    if (!testMethodInfo.IsRunnable)
                    {
                        return(new UnitTestResult[] { new UnitTestResult(UnitTestOutcome.NotRunnable, testMethodInfo.NotRunnableReason) });
                    }

                    return(new TestMethodRunner(testMethodInfo, testMethod, testContext, MSTestSettings.CurrentSettings.CaptureDebugTraces).Execute());
                }
            }
            catch (TypeInspectionException ex)
            {
                // Catch any exception thrown while inspecting the test method and return failure.
                return(new UnitTestResult[] { new UnitTestResult(UnitTestOutcome.Failed, ex.Message) });
            }
        }
Example #3
0
        private void ExecuteTestsWithTestRunner(
            IEnumerable <TestCase> tests,
            IRunContext runContext,
            ITestExecutionRecorder testExecutionRecorder,
            string source,
            IDictionary <string, object> sourceLevelParameters,
            UnitTestRunner testRunner)
        {
            var testExecutionRecorderWrapper = new TestExecutionRecorderWrapper(testExecutionRecorder);

            foreach (var currentTest in tests)
            {
                if (this.cancellationToken != null && this.cancellationToken.Canceled)
                {
                    break;
                }

                var unitTestElement = currentTest.ToUnitTestElement(source);

                // Fire a RecordStart here even though we also fire RecordStart inside TestMethodInfo
                // this is to ensure we fire start events for error cases. This is acceptable because
                // vstest ignores multiple start events.
                testExecutionRecorder.RecordStart(currentTest);

                var startTime = DateTimeOffset.Now;

                PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo(
                    "Executing test {0}",
                    unitTestElement.TestMethod.Name);

                // Run single test passing test context properties to it.
                var tcmProperties         = TcmTestPropertiesProvider.GetTcmProperties(currentTest);
                var testContextProperties = this.GetTestContextProperties(tcmProperties, sourceLevelParameters);
                var unitTestResult        = testRunner.RunSingleTest(unitTestElement.TestMethod, testExecutionRecorderWrapper, testContextProperties);

                PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo(
                    "Executed test {0}",
                    unitTestElement.TestMethod.Name);

                var endTime = DateTimeOffset.Now;

                this.SendTestResults(currentTest, unitTestResult, startTime, endTime, testExecutionRecorder);
            }
        }
Example #4
0
        /// <summary>
        /// Resolve the test method. The function will try to
        /// find a function that has the method name with 0 parameters. If the function
        /// cannot be found, or a function is found that returns non-void, the result is
        /// set to error.
        /// </summary>
        /// <param name="testMethod"> The test Method. </param>
        /// <param name="testClassInfo"> The test Class Info. </param>
        /// <param name="testContext"> The test Context. </param>
        /// <param name="testExecutionRecorder">A instance of TestExecutionRecorderWrapper used to log test execution.</param>
        /// <param name="captureDebugTraces"> Indicates whether the test method should capture debug traces.</param>
        /// <returns>
        /// The TestMethodInfo for the given test method. Null if the test method could not be found.
        /// </returns>
        private TestMethodInfo ResolveTestMethod(TestMethod testMethod, TestClassInfo testClassInfo, ITestContext testContext, TestExecutionRecorderWrapper testExecutionRecorder, bool captureDebugTraces)
        {
            Debug.Assert(testMethod != null, "testMethod is Null");
            Debug.Assert(testClassInfo != null, "testClassInfo is Null");

            var methodInfo = this.GetMethodInfoForTestMethod(testMethod, testClassInfo);

            if (methodInfo == null)
            {
                // Means the specified test method could not be found.
                return(null);
            }

            var expectedExceptionAttribute = this.reflectionHelper.ResolveExpectedExceptionHelper(methodInfo, testMethod);
            var timeout = this.GetTestTimeout(methodInfo, testMethod);

            var testMethodOptions = new TestMethodOptions()
            {
                Timeout = timeout, Executor = this.GetTestMethodAttribute(methodInfo, testClassInfo), ExpectedException = expectedExceptionAttribute, TestContext = testContext, CaptureDebugTraces = captureDebugTraces
            };
            var testMethodInfo = new TestMethodInfo(methodInfo, testClassInfo, testMethodOptions, testExecutionRecorder);

            this.SetCustomProperties(testMethodInfo, testContext);

            return(testMethodInfo);
        }
Example #5
0
        /// <summary>
        /// Get the test method info corresponding to the parameter test Element
        /// </summary>
        /// <param name="testMethod"> The test Method. </param>
        /// <param name="testContext"> The test Context. </param>
        /// <param name="testExecutionRecorder">A instance of TestExecutionRecorderWrapper used to log test execution.</param>
        /// <param name="captureDebugTraces"> Indicates whether the test method should capture debug traces.</param>
        /// <returns> The <see cref="TestMethodInfo"/>. </returns>
        public TestMethodInfo GetTestMethodInfo(TestMethod testMethod, ITestContext testContext, TestExecutionRecorderWrapper testExecutionRecorder, bool captureDebugTraces)
        {
            if (testMethod == null)
            {
                throw new ArgumentNullException("testMethod");
            }

            if (testContext == null)
            {
                throw new ArgumentNullException("testContext");
            }

            // Get the classInfo (This may throw as GetType calls assembly.GetType(..,true);)
            var testClassInfo = this.GetClassInfo(testMethod);

            if (testClassInfo == null)
            {
                // This means the class containing the test method could not be found.
                // Return null so we return a not found result.
                return(null);
            }

            // Get the testMethod
            return(this.ResolveTestMethod(testMethod, testClassInfo, testContext, testExecutionRecorder, captureDebugTraces));
        }