Ejemplo n.º 1
0
        public void RunTests(IEnumerable <string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)
        {
            ValidateArg.NotNull(sources, "sources");
            ValidateArg.NotNull(runContext, "runContext");
            ValidateArg.NotNull(frameworkHandle, "frameworkHandle");

            _cancelRequested.Reset();

            var executorUri = new Uri(PythonConstants.TestExecutorUriString);
            var tests       = new List <TestCase>();
            var doc         = new XPathDocument(new StringReader(runContext.RunSettings.SettingsXml));

            foreach (var t in TestReader.ReadTests(doc, new HashSet <string>(sources, StringComparer.OrdinalIgnoreCase), m => {
                frameworkHandle?.SendMessage(TestMessageLevel.Warning, m);
            }))
            {
                tests.Add(new TestCase(t.FullyQualifiedName, executorUri, t.SourceFile)
                {
                    DisplayName  = t.DisplayName,
                    LineNumber   = t.LineNo,
                    CodeFilePath = t.SourceFile
                });
            }

            if (_cancelRequested.WaitOne(0))
            {
                return;
            }

            RunTestCases(tests, runContext, frameworkHandle);
        }
Ejemplo n.º 2
0
            private IEnumerable <KeyValuePair <string, TestCase> > GetTestCases()
            {
                var moduleCache = new Dictionary <string, ModulePath>();

                foreach (var test in _tests)
                {
                    string testFile, testClass, testMethod;
                    TestReader.ParseFullyQualifiedTestName(
                        test.FullyQualifiedName,
                        out testFile,
                        out testClass,
                        out testMethod
                        );

                    ModulePath module;
                    if (!moduleCache.TryGetValue(testFile, out module))
                    {
                        string testFilePath = PathUtils.GetAbsoluteFilePath(_settings.ProjectHome, testFile);
                        moduleCache[testFile] = module = ModulePath.FromFullPath(testFilePath);
                    }

                    yield return(new KeyValuePair <string, TestCase>("{0}.{1}.{2}".FormatInvariant(
                                                                         module.ModuleName,
                                                                         testClass,
                                                                         testMethod
                                                                         ), test));
                }
            }
Ejemplo n.º 3
0
        public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle) {
            if (sources == null) {
                throw new ArgumentNullException(nameof(sources));
            }

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

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

            _cancelRequested.Reset();

            var executorUri = new Uri(PythonConstants.TestExecutorUriString);
            var tests = new List<TestCase>();
            var doc = Read(runContext.RunSettings.SettingsXml);
            foreach (var t in TestReader.ReadTests(doc, new HashSet<string>(sources, StringComparer.OrdinalIgnoreCase), m => {
                frameworkHandle?.SendMessage(TestMessageLevel.Warning, m);
            })) {
                tests.Add(new TestCase(t.FullyQualifiedName, executorUri, t.SourceFile) {
                    DisplayName = t.DisplayName,
                    LineNumber = t.LineNo,
                    CodeFilePath = t.SourceFile
                });
            }

            if (_cancelRequested.WaitOne(0)) {
                return;
            }

            RunTestCases(tests, runContext, frameworkHandle);
        }
Ejemplo n.º 4
0
        private static string GetCoveragePath(IEnumerable<TestCase> tests) {
            string bestFile = null, bestClass = null, bestMethod = null;

            // Try and generate a friendly name for the coverage report.  We use
            // the filename, class, and method.  We include each one if we're
            // running from a single filename/class/method.  When we have multiple
            // we drop the identifying names.  If we have multiple files we
            // go to the top level directory...  If all else fails we do "pycov".
            foreach (var test in tests) {
                string testFile, testClass, testMethod;
                TestReader.ParseFullyQualifiedTestName(
                    test.FullyQualifiedName,
                    out testFile,
                    out testClass,
                    out testMethod
                );

                bestFile = UpdateBestFile(bestFile, test.CodeFilePath);
                if (bestFile != test.CodeFilePath) {
                    // Different files, don't include class/methods even
                    // if they happen to be the same.
                    bestClass = bestMethod = "";
                }

                bestClass = UpdateBest(bestClass, testClass);
                bestMethod = UpdateBest(bestMethod, testMethod);
            }

            string filename = "";

            if (!String.IsNullOrWhiteSpace(bestFile)) {
                if (ModulePath.IsPythonSourceFile(bestFile)) {
                    filename = ModulePath.FromFullPath(bestFile).ModuleName;
                } else {
                    filename = Path.GetFileName(bestFile);
                }
            } else {
                filename = "pycov";
            }

            if (!String.IsNullOrWhiteSpace(bestClass)) {
                filename += "_" + bestClass;
            }

            if (!String.IsNullOrWhiteSpace(bestMethod)) {
                filename += "_" + bestMethod;
            }

            filename += "_" + DateTime.Now.ToString("s").Replace(':', '_');

            return Path.Combine(Path.GetTempPath(), filename);
        }
Ejemplo n.º 5
0
            private string[] GetArguments()
            {
                var arguments = new List <string>();

                arguments.Add(TestLauncherPath);
                foreach (var test in _tests)
                {
                    arguments.Add("-t");
                    string testFile, testClass, testMethod;
                    TestReader.ParseFullyQualifiedTestName(
                        test.FullyQualifiedName,
                        out testFile,
                        out testClass,
                        out testMethod
                        );

                    string testFilePath = PathUtils.GetAbsoluteFilePath(_settings.ProjectHome, testFile);
                    var    module       = ModulePath.FromFullPath(testFilePath);

                    arguments.Add(
                        string.Format("{0}.{1}.{2}",
                                      module.ModuleName,
                                      testClass,
                                      testMethod
                                      )
                        );
                }

                if (_codeCoverageFile != null)
                {
                    arguments.Add("--coverage");
                    arguments.Add(_codeCoverageFile);
                }

                if (_debugMode == PythonDebugMode.PythonOnly)
                {
                    arguments.AddRange(new[] {
                        "-s", _debugSecret,
                        "-p", _debugPort.ToString()
                    });
                }
                else if (_debugMode == PythonDebugMode.PythonAndNative)
                {
                    arguments.Add("-x");
                }

                arguments.Add("-r");
                arguments.Add(((IPEndPoint)_socket.LocalEndPoint).Port.ToString());
                return(arguments.ToArray());
            }
Ejemplo n.º 6
0
        static void DiscoverTests(IEnumerable <string> sources, IMessageLogger logger, ITestCaseDiscoverySink discoverySink, IRunSettings settings)
        {
            var sourcesSet = new HashSet <string>(sources, StringComparer.OrdinalIgnoreCase);

            var executorUri = new Uri(PythonConstants.TestExecutorUriString);
            // Test list is sent to us via our run settings which we use to smuggle the
            // data we have in our analysis process.
            var doc = Read(settings.SettingsXml);

            foreach (var t in TestReader.ReadTests(doc, sourcesSet, m => {
                logger?.SendMessage(TestMessageLevel.Warning, m);
            }))
            {
                var tc = new TestCase(t.FullyQualifiedName, executorUri, t.SourceFile)
                {
                    DisplayName  = t.DisplayName,
                    LineNumber   = t.LineNo,
                    CodeFilePath = t.FileName
                };

                discoverySink.SendTestCase(tc);
            }
        }
Ejemplo n.º 7
0
            private void ConectionReceivedEvent(object sender, EventReceivedEventArgs e)
            {
                switch (e.Name)
                {
                case TP.ResultEvent.Name:
                    var         result  = (TP.ResultEvent)e.Event;
                    TestOutcome outcome = TestOutcome.None;
                    switch (result.outcome)
                    {
                    case "passed": outcome = TestOutcome.Passed; break;

                    case "failed": outcome = TestOutcome.Failed; break;

                    case "skipped": outcome = TestOutcome.Skipped; break;
                    }

                    var testResult = new TestResult(_curTest);
                    RecordEnd(
                        _frameworkHandle,
                        _curTest,
                        testResult,
                        _stdOut.ToString(),
                        _stdErr.ToString(),
                        outcome,
                        result
                        );

                    _stdOut.Clear();
                    _stdErr.Clear();
                    break;

                case TP.StartEvent.Name:
                    var start = (TP.StartEvent)e.Event;
                    _curTest = null;
                    foreach (var test in _tests)
                    {
                        string testFile, testClass, testMethod;
                        TestReader.ParseFullyQualifiedTestName(
                            test.FullyQualifiedName,
                            out testFile,
                            out testClass,
                            out testMethod
                            );

                        string testFilePath = PathUtils.GetAbsoluteFilePath(_settings.ProjectHome, test.CodeFilePath);
                        var    modulePath   = ModulePath.FromFullPath(testFilePath);

                        if (start.test == modulePath.ModuleName + "." + testClass + "." + testMethod)
                        {
                            _curTest = test;
                            break;
                        }
                    }

                    if (_curTest != null)
                    {
                        _frameworkHandle.RecordStart(_curTest);
                    }
                    else
                    {
                        Warning(Strings.Test_UnexpectedResult.FormatUI(start.classname, start.method));
                    }
                    break;

                case TP.StdErrEvent.Name:
                    var err = (TP.StdErrEvent)e.Event;
                    _stdErr.Append(err.content);
                    break;

                case TP.StdOutEvent.Name:
                    var outp = (TP.StdOutEvent)e.Event;
                    _stdOut.Append(outp.content);
                    break;

                case TP.DoneEvent.Name:
                    _done.Set();
                    break;
                }
            }