public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext,
            IFrameworkHandle frameworkHandle)
        {
            _mCancelled = false;
            SetupExecutionPolicy();
            foreach (var test in tests)
            {
                if (_mCancelled) break;

                var testFramework = test.FullyQualifiedName.Split(new[] { "||" }, StringSplitOptions.None)[0];

                var executor = _testExecutors.FirstOrDefault(
                    m => m.TestFramework.Equals(testFramework, StringComparison.OrdinalIgnoreCase));

                if (executor == null)
                {
                    frameworkHandle.SendMessage(TestMessageLevel.Error, String.Format("Unknown test executor: {0}", testFramework));
                    return;
                }

                var testResult = new TestResult(test);
                testResult.Outcome = TestOutcome.Failed;
                testResult.ErrorMessage = "Unexpected error! Failed to run tests!";

                PowerShellTestResult testResultData = null;
                var testOutput = new StringBuilder();

                try
                {
                    var testAdapter = new TestAdapterHost();
                    testAdapter.HostUi.OutputString = s => testOutput.Append(s);

                    var runpsace = RunspaceFactory.CreateRunspace(testAdapter);
                    runpsace.Open();

                    using (var ps = PowerShell.Create())
                    {
                        ps.Runspace = runpsace;

                        testResultData = executor.RunTest(ps, test, runContext);
                    }
                }
                catch (Exception ex)
                {
                    testResult.Outcome = TestOutcome.Failed;
                    testResult.ErrorMessage = ex.Message;
                    testResult.ErrorStackTrace = ex.StackTrace;
                }

                if (testResultData != null)
                {
                    testResult.Outcome = testResultData.Outcome;
                    testResult.ErrorMessage = testResultData.ErrorMessage;
                    testResult.ErrorStackTrace = testResultData.ErrorStacktrace;
                }

                if (testOutput.Length > 0)
                {
                    frameworkHandle.SendMessage(TestMessageLevel.Informational, testOutput.ToString());
                }

                frameworkHandle.RecordResult(testResult);
            }
        }
        public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext,
               IFrameworkHandle frameworkHandle)
        {
            _mCancelled = false;
            SetupExecutionPolicy();
            foreach (var test in tests)
            {
                if (_mCancelled) break;

                var testFramework = test.FullyQualifiedName.Split(new[] { "||" }, StringSplitOptions.None)[0];

                var executor = _testExecutors.FirstOrDefault(
                    m => m.TestFramework.Equals(testFramework, StringComparison.OrdinalIgnoreCase));

                if (executor == null)
                {
                    frameworkHandle.SendMessage(TestMessageLevel.Error, String.Format("Unknown test executor: {0}", testFramework));
                    return;
                }

                var testResult = new TestResult(test);
                testResult.Outcome = TestOutcome.Failed;
                testResult.ErrorMessage = "Unexpected error! Failed to run tests!";

                PowerShellTestResult testResultData = null;
                var testOutput = new StringBuilder();

                try
                {
                    var testAdapter = new TestAdapterHost();
                    testAdapter.HostUi.OutputString = s => testOutput.Append(s);

                    var runpsace = RunspaceFactory.CreateRunspace(testAdapter);
                    runpsace.Open();

                    using (var ps = PowerShell.Create())
                    {
                        ps.Runspace = runpsace;

                        testResultData = executor.RunTest(ps, test, runContext);
                    }
                }
                catch (Exception ex)
                {
                    testResult.Outcome = TestOutcome.Failed;
                    testResult.ErrorMessage = ex.Message;
                    testResult.ErrorStackTrace = ex.StackTrace;
                }

                if (testResultData != null)
                {
                    testResult.Outcome = testResultData.Outcome;
                    testResult.ErrorMessage = testResultData.ErrorMessage;
                    testResult.ErrorStackTrace = testResultData.ErrorStacktrace;
                }

                if (testOutput.Length > 0)
                {
                    frameworkHandle.SendMessage(TestMessageLevel.Informational, testOutput.ToString());    
                }
                
                frameworkHandle.RecordResult(testResult);
            }

        }
Пример #3
0
        public void RunTests(IEnumerable <TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
        {
            _mCancelled = false;
            SetupExecutionPolicy();

            var testSets = new List <TestCaseSet>();

            foreach (var testCase in tests)
            {
                var describe = testCase.FullyQualifiedName.Split('.').First();
                var codeFile = testCase.CodeFilePath;

                var testSet = testSets.FirstOrDefault(m => m.Describe.Equals(describe, StringComparison.OrdinalIgnoreCase) &&
                                                      m.File.Equals(codeFile, StringComparison.OrdinalIgnoreCase));

                if (testSet == null)
                {
                    testSet = new TestCaseSet(codeFile, describe);
                    testSets.Add(testSet);
                }

                testSet.TestCases.Add(testCase);
            }

            foreach (var testSet in testSets)
            {
                if (_mCancelled)
                {
                    break;
                }

                var testOutput = new StringBuilder();

                try
                {
                    var testAdapter = new TestAdapterHost();
                    testAdapter.HostUi.OutputString = s =>
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            testOutput.Append(s);
                        }
                    };

                    var runpsace = RunspaceFactory.CreateRunspace(testAdapter);
                    runpsace.Open();

                    using (var ps = PowerShell.Create())
                    {
                        ps.Runspace = runpsace;
                        RunTestSet(ps, testSet, runContext);

                        foreach (var testResult in testSet.TestResults)
                        {
                            frameworkHandle.RecordResult(testResult);
                        }
                    }
                }
                catch (Exception ex)
                {
                    foreach (var testCase in testSet.TestCases)
                    {
                        var testResult = new TestResult(testCase);
                        testResult.Outcome         = TestOutcome.Failed;
                        testResult.ErrorMessage    = ex.Message;
                        testResult.ErrorStackTrace = ex.StackTrace;
                        frameworkHandle.RecordResult(testResult);
                    }
                }

                if (testOutput.Length > 0)
                {
                    frameworkHandle.SendMessage(TestMessageLevel.Informational, testOutput.ToString());
                }
            }
        }