示例#1
0
        public void RunTests(IEnumerable <TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
        {
            var CatchExe = tests.First().Source;
            var timer    = Stopwatch.StartNew();

            // Get a list of all test case names
            var listOfTestCases = tests.Aggregate("", (acc, test) => acc + test.DisplayName + "\n");

            // Use the directory of the executable as the working directory.
            string workingDirectory = System.IO.Path.GetDirectoryName(CatchExe);

            if (workingDirectory == "")
            {
                workingDirectory = ".";
            }

            // Write them to the input file for Catch runner
            string caseFile = "test.cases";

            System.IO.File.WriteAllText(
                workingDirectory + System.IO.Path.DirectorySeparatorChar + caseFile,
                listOfTestCases);
            string originalDirectory = Directory.GetCurrentDirectory();

            // Execute the tests
            IList <string> output_text;

            string arguments = "-r xml --durations yes --input-file=" + caseFile;

            if (runContext.IsBeingDebugged)
            {
                output_text = ProcessRunner.RunDebugProcess(frameworkHandle, CatchExe, arguments, workingDirectory);
            }
            else
            {
                output_text = ProcessRunner.RunProcess(CatchExe, arguments, workingDirectory);
            }

            timer.Stop();
            frameworkHandle.SendMessage(TestMessageLevel.Informational, "Overall time " + timer.Elapsed.ToString());

            // Output as a single string.
            string output = output_text.Aggregate("", (acc, add) => acc + add);

            System.IO.MemoryStream reader = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(output));
            var serializer  = new XmlSerializer(typeof(CatchTestAdapter.Tests.Catch));
            var catchResult = (CatchTestAdapter.Tests.Catch)serializer.Deserialize(reader);

            foreach (var testCase in catchResult.TestCases)
            {
                // Find the matching test case
                var test       = tests.Where((test_case) => test_case.DisplayName == testCase.Name).ElementAt(0);
                var testResult = new TestResult(test);
                // Add the test execution time provided by Catch to the result.
                var testTime = testCase.Result.Duration;
                testResult.Duration = TimeSpan.FromSeconds(Double.Parse(testTime, CultureInfo.InvariantCulture));
                if (testCase.Result.Success == "true")
                {
                    testResult.Outcome = TestOutcome.Passed;
                }
                else
                {
                    // Mark failure.
                    testResult.Outcome = TestOutcome.Failed;

                    // Parse the failure to a flat result.
                    List <FlatResult> failures = GetFlatFailure(testCase);
                    testResult.ErrorMessage = $"{Environment.NewLine}";
                    for (int i = 1; i <= failures.Count; ++i)
                    {
                        var failure = failures[i - 1];
                        // Populate the error message.
                        var newline = failure.SectionPath.IndexOf("\n");
                        if (newline != -1)
                        {
                            // Remove first line of the SectionPath, which is the test case name.
                            failure.SectionPath      = failure.SectionPath.Substring(failure.SectionPath.IndexOf("\n") + 1);
                            testResult.ErrorMessage += $"#{i} - {failure.SectionPath}{Environment.NewLine}{failure.Expression}{Environment.NewLine}";
                        }
                        else
                        {
                            testResult.ErrorMessage += $"#{i} - {failure.Expression}{Environment.NewLine}";
                        }
                        // And the error stack.
                        testResult.ErrorStackTrace += $"at #{i} - {test.DisplayName}() in {failure.FilePath}:line {failure.LineNumber}{Environment.NewLine}";
                    }
                }
                // Finally record the result.
                frameworkHandle.RecordResult(testResult);
            }
            // Remove the temporary input file.
            System.IO.File.Delete(caseFile);
        }