Example #1
0
        private void ExecuteTestsWithTestRunner(
            IEnumerable <TestCase> tests,
            IRunContext runContext,
            ITestExecutionRecorder testExecutionRecorder,
            string source,
            IDictionary <string, object> sourceLevelParameters,
            UnitTestRunner testRunner)
        {
            foreach (var currentTest in tests)
            {
                if (this.cancellationToken != null && this.cancellationToken.Canceled)
                {
                    break;
                }

                var unitTestElement = currentTest.ToUnitTestElement(source);
                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, testContextProperties);

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

                var endTime = DateTimeOffset.Now;

                this.SendTestResults(currentTest, unitTestResult, startTime, endTime, testExecutionRecorder);
            }
        }
        private void InitializeClassCleanupManager(
            string source,
            UnitTestRunner testRunner,
            ICollection <TestCase> testsToRun,
            TestAssemblySettings sourceSettings)
        {
            var unitTestElements = testsToRun.Select(e => e.ToUnitTestElement(source)).ToArray();

            testRunner.InitializeClassCleanupManager(unitTestElements, sourceSettings.ClassCleanupLifecycle);
        }
Example #3
0
 private void InitializeClassCleanupManager(string source, UnitTestRunner testRunner, ICollection <TestCase> testsToRun, TestAssemblySettings sourceSettings)
 {
     try
     {
         var unitTestElements = testsToRun.Select(e => e.ToUnitTestElement(source)).ToArray();
         testRunner.InitializeClassCleanupManager(unitTestElements, (int)sourceSettings.ClassCleanupLifecycle);
     }
     catch (Exception ex)
     {
         // source might not support this if it's legacy make sure it's supported by checking for the type
         if (ex.GetType().FullName != "System.Runtime.Remoting.RemotingException")
         {
             throw;
         }
     }
 }
Example #4
0
        private void RunCleanup(
            ITestExecutionRecorder testExecutionRecorder,
            UnitTestRunner testRunner)
        {
            // All cleanups (Class and Assembly) run at the end of test execution. Failures in these cleanup
            // methods will be reported as Warnings.
            PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executing cleanup methods.");
            var cleanupResult = testRunner.RunCleanup();

            PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executed cleanup methods.");
            if (cleanupResult != null)
            {
                // Do not attach the standard output and error messages to any test result. It is not
                // guaranteed that a test method of same class would have run last. We will end up
                // adding stdout to test method of another class.
                this.LogCleanupResult(testExecutionRecorder, cleanupResult);
            }
        }
Example #5
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 #6
0
        private void ExecuteTestsWithTestRunner(
            IEnumerable <TestCase> tests,
            IRunContext runContext,
            ITestExecutionRecorder testExecutionRecorder,
            string source,
            UnitTestRunner testRunner)
        {
            TestCase test = null;

            UnitTestResult[] unitTestResult = null;

            var startTime = DateTimeOffset.MinValue;
            var endTime   = DateTimeOffset.MinValue;

            bool filterHasError;
            var  filterExpression = this.TestMethodFilter.GetFilterExpression(
                runContext,
                testExecutionRecorder,
                out filterHasError);

            if (!filterHasError)
            {
                foreach (var currentTest in tests)
                {
                    // Skip test if not fitting filter criteria.
                    if (filterExpression != null && filterExpression.MatchTestCase(
                            currentTest,
                            (p) => this.TestMethodFilter.PropertyValueProvider(currentTest, p)) == false)
                    {
                        continue;
                    }

                    // Send previous test result.
                    this.SendTestResults(test, unitTestResult, startTime, endTime, testExecutionRecorder);

                    if (this.cancellationToken != null && this.cancellationToken.Canceled)
                    {
                        break;
                    }

                    var unitTestElement = currentTest.ToUnitTestElement(source);
                    testExecutionRecorder.RecordStart(currentTest);

                    startTime = DateTimeOffset.Now;

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

                    if (unitTestElement.Ignored)
                    {
                        unitTestResult = new[] { new UnitTestResult(UnitTestOutcome.Ignored, null) };
                    }
                    else
                    {
                        // this is done so that appropriate values of testcontext properties are set at source level
                        // and are merged with session level parameters
                        var sourceLevelParameters = PlatformServiceProvider.Instance.SettingsProvider.GetProperties(source);

                        if (this.sessionParameters != null && this.sessionParameters.Count > 0)
                        {
                            sourceLevelParameters = sourceLevelParameters.Concat(this.sessionParameters).ToDictionary(x => x.Key, x => x.Value);
                        }

                        unitTestResult = testRunner.RunSingleTest(unitTestElement.TestMethod, sourceLevelParameters);
                    }

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

                    endTime = DateTimeOffset.Now;
                    test    = currentTest;
                }
            }

            IList <string> warnings = null;

            try
            {
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executing cleanup methods.");
                var cleanupResult = testRunner.RunCleanup();
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Executed cleanup methods.");

                if (cleanupResult != null)
                {
                    warnings = cleanupResult.Warnings;

                    if (unitTestResult?.Length > 0)
                    {
                        var lastResult = unitTestResult[unitTestResult.Length - 1];
                        lastResult.StandardOut   += cleanupResult.StandardOut;
                        lastResult.StandardError += cleanupResult.StandardError;
                        lastResult.DebugTrace    += cleanupResult.DebugTrace;
                    }
                }
            }
            finally
            {
                // Send last test result
                this.SendTestResults(test, unitTestResult, startTime, endTime, testExecutionRecorder);
            }

            this.LogWarnings(testExecutionRecorder, warnings);
        }