예제 #1
0
 public void DefaultTestRunnerFactory_ProjectServiceMissing()
 {
     var service = new DefaultTestRunnerFactory();
     _services.Add(service);
     service.StartService();
     Assert.That(service.Status, Is.EqualTo(ServiceStatus.Error));
 }
예제 #2
0
        /// <summary>
        /// Runs all tests</summary>
        /// <param name="displayName">Name of the test, which is normally the executing
        /// assembly's Location.</param>
        /// <param name="assemblyPaths">List of assemblies to actually test</param>
        /// <returns>0 if tests ran successfully, a negative number otherwise</returns>
        public static int RunAllTests(string displayName, List <string> assemblyPaths)
        {
            TestRunner runner;

            try
            {
                var package = new TestPackage(displayName, assemblyPaths);
                runner = new DefaultTestRunnerFactory().MakeTestRunner(package);
                runner.Load(package);
            }
            catch (System.IO.FileLoadException)
            {
                // likely caused by ATF source zip file downloaded from internet without unblocking it
                Console.WriteLine("NUnit failed to load {0}", displayName);
                Console.WriteLine(@"Possibly need to unblock the downloaded ATF source zip file before unzipping");
                Console.WriteLine(@"(right click on the zip file -> Properties -> Unblock)");
                return(-3);
            }
            catch (Exception)
            {
                return(-2);
            }

            runner.Run(new UnitTestListener());

            if (runner.TestResult.IsFailure)
            {
                return(-1);
            }

            return(0);
        }
        public void DefaultTestRunnerFactory_ProjectServiceMissing()
        {
            var service = new DefaultTestRunnerFactory();

            _services.Add(service);
            service.StartService();
            Assert.That(service.Status, Is.EqualTo(ServiceStatus.Error));
        }
 public void CreateServiceContext()
 {
     _services = new ServiceContext();
     _services.Add(new ProjectService());
     _factory = new DefaultTestRunnerFactory();
     _services.Add(_factory);
     _services.ServiceManager.StartServices();
 }
 public void CreateServiceContext()
 {
     _services = new ServiceContext();
     _services.Add(new ProjectService());
     _factory = new DefaultTestRunnerFactory();
     _services.Add(_factory);
     _services.ServiceManager.InitializeServices();
 }
예제 #6
0
        public IEnumerable <Shared.Results.TestResult> Execute(Options options)
        {
            var package = createPackage(options);

            using (var testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
            {
                return(runTests(options, package, testRunner));
            }
        }
예제 #7
0
        static void Main(string[] args)
        {
            //Common.Logging.LogManager.Adapter
            //log4net.Config.XmlConfigurator.Configure();

            ServiceManager.Services.AddService(new SettingsService());
            ServiceManager.Services.AddService(new DomainManager());
            ServiceManager.Services.AddService(new ProjectService());
            ServiceManager.Services.InitializeServices();

            TextReader inReader    = Console.In;
            TextWriter outWriter   = Console.Out;
            TextWriter errorWriter = Console.Error;

            EventListener testListener = new EventCollector(inReader, outWriter, errorWriter);

            try
            {
                var assembly  = typeof(com.espertech.esper.client.TestConfiguration).Assembly.Location;
                var framework = RuntimeFramework.CurrentFramework;

                var testPackage = new TestPackage(assembly);
                testPackage.TestName = null;
                testPackage.Settings["DomainUsage"]       = DomainUsage.Single;
                testPackage.Settings["ProcessModel"]      = ProcessModel.Single;
                testPackage.Settings["ShadowCopyFiles"]   = false;
                testPackage.Settings["UseThreadedRunner"] = true;
                testPackage.Settings["DefaultTimeout"]    = 0;
                testPackage.Settings["RuntimeFramework"]  = framework;

                var testFilter = TestFilter.Empty;

                if (args.Length > 0)
                {
                    var nameFilter = new SimpleNameFilter();
                    nameFilter.Add(args[0]);
                    testFilter = nameFilter;
                    Console.WriteLine("Using SimpleNameFilter");
                }

                using (var testRunner = new DefaultTestRunnerFactory().MakeTestRunner(testPackage))
                {
                    testRunner.Load(testPackage);

                    Console.Error.WriteLine("{0}: Testing begins", DateTime.Now.Ticks);
                    testRunner.Run(testListener, testFilter, true, LoggingThreshold.Off);
                    Console.Error.WriteLine("{0}: Testing ends", DateTime.Now.Ticks);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(">> Error");
                Console.WriteLine(e);
            }

            //Console.ReadLine();
        }
예제 #8
0
파일: Program.cs 프로젝트: zparr/ATF
        private static TestRunner MakeTestRunner(string testAssemblyName)
        {
            TestPackage package = new TestPackage(testAssemblyName);
            TestRunner  runner  = new DefaultTestRunnerFactory().MakeTestRunner(package);

            runner.Load(package);

            return(runner);
        }
예제 #9
0
 public void CreateServiceContext()
 {
     _services = new ServiceContext();
     _services.Add(new ExtensionService());
     _services.Add(new FakeProjectService());
     _factory = new DefaultTestRunnerFactory();
     _services.Add(_factory);
     _services.ServiceManager.StartServices();
 }
        private TestResults RunTests(TestPackage package, TestFilter filter, Messages.OnMessage[] subscribers)
        {
            NUnit.Core.TestResult result;
            ProcessModel          processModel     = package.Settings.Contains("ProcessModel") ? (ProcessModel)package.Settings["ProcessModel"] : ProcessModel.Default;
            DomainUsage           domainUsage      = package.Settings.Contains("DomainUsage") ? (DomainUsage)package.Settings[(object)"DomainUsage"] : DomainUsage.Default;
            RuntimeFramework      runtimeFramework = package.Settings.Contains("RuntimeFramework") ? (RuntimeFramework)package.Settings["RuntimeFramework"] : RuntimeFramework.CurrentFramework;

            using (new SaveConsoleOutput())
                using (TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
                    using (var ms = new MemoryStream())
                        using (var output = CreateOutputWriter(ms))
                        {
                            try
                            {
                                TestEventHandler eventCollector = new TestEventHandler(_options, output, subscribers);
                                testRunner.Load(package);
                                if (testRunner.Test == null)
                                {
                                    testRunner.Unload();
                                    return(new TestResults(TestResults.Code.FixtureNotFound, "Unable to locate fixture"));
                                }
                                result = testRunner.Run(eventCollector, filter, false, LoggingThreshold.All);
                                var summary = eventCollector.GetSummary();

                                output.Flush();
                                if (summary.UnexpectedError)
                                {
                                    return(new TestResults(TestResults.Code.UnexpectedError, GetResultText(ms), summary));
                                }

                                return(new TestResults(summary.InvalidAssemblies > 0
                            ? TestResults.Code.InvalidAssembly
                            : GetCode(summary.FailureCount + summary.ErrorCount + summary.InvalidCount),
                                                       GetResultText(ms), summary));
                            }
                            catch (FileNotFoundException ex)
                            {
                                output.WriteLine(ex.Message);
                                output.Flush();
                                return(new TestResults(TestResults.Code.InvalidAssembly, GetResultText(ms)));
                            }
                            catch (DirectoryNotFoundException ex)
                            {
                                output.WriteLine(ex.Message);
                                output.Flush();
                                return(new TestResults(TestResults.Code.InvalidAssembly, GetResultText(ms)));
                            }
                            catch (Exception ex)
                            {
                                output.WriteLine(ex.ToString());
                                output.Flush();
                                return(new TestResults(TestResults.Code.UnexpectedError, GetResultText(ms)));
                            }
                        }
        }
예제 #11
0
        public void SetUp()
        {
            var services = new ServiceContext();

#if !NETCOREAPP1_1
            services.Add(new ExtensionService());
            services.Add(new FakeProjectService());
#endif
            _factory = new DefaultTestRunnerFactory();
            services.Add(_factory);
            _factory.StartService();
        }
        public void SetUp()
        {
            var services = new ServiceContext();

#if !NETCOREAPP1_1
            services.Add(new ExtensionService());
            var projectService = new FakeProjectService();
            projectService.Add(TestPackageFactory.FakeProject, "a.dll", "b.dll");
            services.Add(projectService);
#endif
            _factory = new DefaultTestRunnerFactory();
            services.Add(_factory);
            _factory.StartService();
        }
예제 #13
0
        public MasterTestRunner(TestPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            TestPackage = package;

            // Get references to the services we use
            var runnerFactory = new DefaultTestRunnerFactory();

            _engineRunner = runnerFactory.MakeTestRunner(package);

            InitializePackage();
        }
예제 #14
0
        public void SetUp()
        {
            _services = new ServiceContext();
            _services.Add(new ExtensionService());
            var projectService = new FakeProjectService();

            ((IService)projectService).StartService();
            projectService.Add(TestPackageFactory.FakeProject, "a.dll", "b.dll");
            _services.Add(projectService);
            Assert.That(((IService)projectService).Status, Is.EqualTo(ServiceStatus.Started));
            _factory = new DefaultTestRunnerFactory();
            _services.Add(_factory);
            _factory.StartService();
            Assert.That(_factory.Status, Is.EqualTo(ServiceStatus.Started));

            var fakeRuntimeService = new FakeRuntimeService();

            ((IService)fakeRuntimeService).StartService();
            _services.Add(fakeRuntimeService);
            Assert.That(((IService)fakeRuntimeService).Status, Is.EqualTo(ServiceStatus.Started));
        }
예제 #15
0
        public int Execute(ExtendedConsoleOptions options)
        {
            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = options.output != null && options.output != string.Empty;

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(options.output);
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = options.err != null && options.err != string.Empty;

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(options.err);
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TextWriter testResultWriter = null;

            if (options.IsResults)
            {
                testResultWriter = new StreamWriter(options.results, false, Encoding.UTF8);
                ((StreamWriter)testResultWriter).AutoFlush = true;
            }

            TestPackage package = MakeTestPackage(options);

            Console.WriteLine("ProcessModel: {0}    DomainUsage: {1}",
                              package.Settings.Contains("ProcessModel")
                    ? package.Settings["ProcessModel"]
                    : "Default",
                              package.Settings.Contains("DomainUsage")
                    ? package.Settings["DomainUsage"]
                    : "Default");

            Console.WriteLine("Execution Runtime: {0}",
                              package.Settings.Contains("RuntimeFramework")
                    ? package.Settings["RuntimeFramework"]
                    : "Default");

            TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package);

            testRunner.Load(package);

            try
            {
                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(FIXTURE_NOT_FOUND);
                }

                ExtendedEventCollector collector = new ExtendedEventCollector(options, outWriter, errorWriter, testResultWriter);

                TestFilter testFilter = TestFilter.Empty;
                if (options.run != null && options.run != string.Empty)
                {
                    Console.WriteLine("Selected test(s): " + options.run);
                    testFilter = new SimpleNameFilter(options.run);
                }

                if (options.include != null && options.include != string.Empty)
                {
                    Console.WriteLine("Included categories: " + options.include);
                    TestFilter includeFilter = new CategoryExpression(options.include).Filter;
                    if (testFilter.IsEmpty)
                    {
                        testFilter = includeFilter;
                    }
                    else
                    {
                        testFilter = new AndFilter(testFilter, includeFilter);
                    }
                }

                if (options.exclude != null && options.exclude != string.Empty)
                {
                    Console.WriteLine("Excluded categories: " + options.exclude);
                    TestFilter excludeFilter = new NotFilter(new CategoryExpression(options.exclude).Filter);
                    if (testFilter.IsEmpty)
                    {
                        testFilter = excludeFilter;
                    }
                    else if (testFilter is AndFilter)
                    {
                        ((AndFilter)testFilter).Add(excludeFilter);
                    }
                    else
                    {
                        testFilter = new AndFilter(testFilter, excludeFilter);
                    }
                }

                if (testFilter is NotFilter)
                {
                    ((NotFilter)testFilter).TopLevel = true;
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, testFilter);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }
                    if (redirectError)
                    {
                        errorWriter.Close();
                    }
                    if (options.IsResults)
                    {
                        testResultWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                int returnCode = UNEXPECTED_ERROR;

                if (result != null)
                {
                    string           xmlOutput = CreateXmlOutput(result);
                    ResultSummarizer summary   = new ResultSummarizer(result);

                    if (options.xmlConsole)
                    {
                        Console.WriteLine(xmlOutput);
                    }
                    else
                    {
                        WriteSummaryReport(summary);
                        if (summary.ErrorsAndFailures > 0)
                        {
                            WriteErrorsAndFailuresReport(result);
                        }
                        if (summary.TestsNotRun > 0)
                        {
                            WriteNotRunReport(result);
                        }
                    }

                    // Write xml output here
                    string xmlResultFile = options.xml == null || options.xml == string.Empty
                        ? "TestResult.xml" : options.xml;

                    if (!String.IsNullOrEmpty(options.xml))
                    {
                        using (StreamWriter writer = new StreamWriter(xmlResultFile))
                        {
                            writer.Write(xmlOutput);
                        }
                    }
                    returnCode = summary.ErrorsAndFailures;
                }

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    returnCode = UNEXPECTED_ERROR;
                }

                return(returnCode);
            }
            finally
            {
                testRunner.Unload();
            }
        }
예제 #16
0
 public void Init()
 {
     factory = new DefaultTestRunnerFactory();
     package = new TestPackage(testDll);
 }
예제 #17
0
 public AbstractTestRunner(TestPackage package)
 {
     TestRunnerFactory = new DefaultTestRunnerFactory();
     TestPackage       = package;
 }
예제 #18
0
 public void Init()
 {
     factory = new DefaultTestRunnerFactory();
     package = new TestPackage(testDll);
 }
예제 #19
0
        public int Execute(ConsoleOptions options)
        {
            this.workDir = options.work;
            if (workDir == null || workDir == string.Empty)
            {
                workDir = Environment.CurrentDirectory;
            }
            else
            {
                workDir = Path.GetFullPath(workDir);
                if (!Directory.Exists(workDir))
                {
                    Directory.CreateDirectory(workDir);
                }
            }

            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = options.output != null && options.output != string.Empty;

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(Path.Combine(workDir, options.output));
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = options.err != null && options.err != string.Empty;

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(Path.Combine(workDir, options.err));
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TestPackage package = MakeTestPackage(options);

            ProcessModel processModel = package.Settings.Contains("ProcessModel")
                ? (ProcessModel)package.Settings["ProcessModel"]
                : ProcessModel.Default;

            DomainUsage domainUsage = package.Settings.Contains("DomainUsage")
                ? (DomainUsage)package.Settings["DomainUsage"]
                : DomainUsage.Default;

            RuntimeFramework framework = package.Settings.Contains("RuntimeFramework")
                ? (RuntimeFramework)package.Settings["RuntimeFramework"]
                : RuntimeFramework.CurrentFramework;

#if CLR_2_0 || CLR_4_0
            Console.WriteLine("ProcessModel: {0}    DomainUsage: {1}", processModel, domainUsage);

            Console.WriteLine("Execution Runtime: {0}", framework);
#else
            Console.WriteLine("DomainUsage: {0}", domainUsage);

            if (processModel != ProcessModel.Default && processModel != ProcessModel.Single)
            {
                Console.WriteLine("Warning: Ignoring project setting 'processModel={0}'", processModel);
            }

            if (!RuntimeFramework.CurrentFramework.Supports(framework))
            {
                Console.WriteLine("Warning: Ignoring project setting 'runtimeFramework={0}'", framework);
            }
#endif

            using (TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
            {
                if (options.compatibility)
                {
                    Compatibility.Initialize(workDir);
                }

                testRunner.Load(package);

                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(FIXTURE_NOT_FOUND);
                }

                EventCollector collector = new EventCollector(options, outWriter, errorWriter);

                TestFilter testFilter;

                if (!CreateTestFilter(options, out testFilter))
                {
                    return(INVALID_ARG);
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, testFilter, true, LoggingThreshold.Off);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }

                    if (redirectError)
                    {
                        errorWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                int returnCode = UNEXPECTED_ERROR;

                if (result != null)
                {
                    string xmlOutput = CreateXmlOutput(result);
                    if (options.xmlConsole)
                    {
                        Console.WriteLine(xmlOutput);
                    }
                    else
                    {
                        new ResultReporter(result, options).ReportResults();

                        // Check both new and old option forms
                        if (!options.noresult && !options.noxml)
                        {
                            // Write xml output here
                            string xmlResultFile = !string.IsNullOrEmpty(options.result)
                                ? options.result
                                : !string.IsNullOrEmpty(options.xml)
                                    ? options.xml
                                    : "TestResult.xml";

                            using (StreamWriter writer = new StreamWriter(Path.Combine(workDir, xmlResultFile)))
                            {
                                writer.Write(xmlOutput);
                            }
                        }
                    }

                    ResultSummarizer summary = new ResultSummarizer(result);
                    returnCode = summary.Errors + summary.Failures + summary.NotRunnable;
                }

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    returnCode = UNEXPECTED_ERROR;
                }

                return(returnCode);
            }
        }
예제 #20
0
        public TestResult Execute(ConsoleOptions options)
        {
            _workDir = options.work;
            if (string.IsNullOrEmpty(_workDir))
            {
                _workDir = Environment.CurrentDirectory;
            }
            else
            {
                _workDir = Path.GetFullPath(_workDir);
                if (!Directory.Exists(_workDir))
                {
                    Directory.CreateDirectory(_workDir);
                }
            }

            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = !string.IsNullOrEmpty(options.output);

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(Path.Combine(_workDir, options.output));
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = !string.IsNullOrEmpty(options.err);

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(Path.Combine(_workDir, options.err));
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TestPackage package = MakeTestPackage(options);

            ProcessModel processModel = package.Settings.Contains("ProcessModel")
                ? (ProcessModel)package.Settings["ProcessModel"]
                : ProcessModel.Default;

            DomainUsage domainUsage = package.Settings.Contains("DomainUsage")
                ? (DomainUsage)package.Settings["DomainUsage"]
                : DomainUsage.Default;

            RuntimeFramework framework = package.Settings.Contains("RuntimeFramework")
                ? (RuntimeFramework)package.Settings["RuntimeFramework"]
                : RuntimeFramework.CurrentFramework;

#if CLR_2_0 || CLR_4_0
            Console.WriteLine("ProcessModel: {0}    DomainUsage: {1}", processModel, domainUsage);

            Console.WriteLine("Execution Runtime: {0}", framework);
#else
            Console.WriteLine("DomainUsage: {0}", domainUsage);

            if (processModel != ProcessModel.Default && processModel != ProcessModel.Single)
            {
                Console.WriteLine("Warning: Ignoring project setting 'processModel={0}'", processModel);
            }

            if (!RuntimeFramework.CurrentFramework.Supports(framework))
            {
                Console.WriteLine("Warning: Ignoring project setting 'runtimeFramework={0}'", framework);
            }
#endif

            using (NUnit.Core.TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
            {
                testRunner.Load(package);

                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(null);
                }

                EventCollector collector = new EventCollector(options, outWriter, errorWriter);

                TestFilter testFilter;

                if (!CreateTestFilter(options, out testFilter))
                {
                    return(null);
                }

                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    return(testRunner.Run(collector, testFilter, false, LoggingThreshold.Off));
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }

                    if (redirectError)
                    {
                        errorWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }
            }
        }
예제 #21
0
        public int Execute(ConsoleOptions options)
        {
            this.workDir = options.work;
            if (workDir == null || workDir == string.Empty)
            {
                workDir = Environment.CurrentDirectory;
            }
            else
            {
                workDir = Path.GetFullPath(workDir);
                if (!Directory.Exists(workDir))
                {
                    Directory.CreateDirectory(workDir);
                }
            }

            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = options.output != null && options.output != string.Empty;

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(Path.Combine(workDir, options.output));
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = options.err != null && options.err != string.Empty;

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(Path.Combine(workDir, options.err));
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TestPackage package = MakeTestPackage(options);

            ProcessModel processModel = package.Settings.Contains("ProcessModel")
                ? (ProcessModel)package.Settings["ProcessModel"]
                : ProcessModel.Default;

            DomainUsage domainUsage = package.Settings.Contains("DomainUsage")
                ? (DomainUsage)package.Settings["DomainUsage"]
                : DomainUsage.Default;

            RuntimeFramework framework = package.Settings.Contains("RuntimeFramework")
                ? (RuntimeFramework)package.Settings["RuntimeFramework"]
                : RuntimeFramework.CurrentFramework;

#if CLR_2_0 || CLR_4_0
            Console.WriteLine("ProcessModel: {0}    DomainUsage: {1}", processModel, domainUsage);

            Console.WriteLine("Execution Runtime: {0}", framework);
#else
            Console.WriteLine("DomainUsage: {0}", domainUsage);

            if (processModel != ProcessModel.Default && processModel != ProcessModel.Single)
            {
                Console.WriteLine("Warning: Ignoring project setting 'processModel={0}'", processModel);
            }

            if (!RuntimeFramework.CurrentFramework.Supports(framework))
            {
                Console.WriteLine("Warning: Ignoring project setting 'runtimeFramework={0}'", framework);
            }
#endif

            using (TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
            {
                testRunner.Load(package);

                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(FIXTURE_NOT_FOUND);
                }

                EventCollector collector = new EventCollector(options, outWriter, errorWriter);

                TestFilter       testFilter = TestFilter.Empty;
                SimpleNameFilter nameFilter = new SimpleNameFilter();

                if (options.run != null && options.run != string.Empty)
                {
                    Console.WriteLine("Selected test(s): " + options.run);
                    foreach (string name in TestNameParser.Parse(options.run))
                    {
                        nameFilter.Add(name);
                    }
                    testFilter = nameFilter;
                }

                if (options.runlist != null && options.runlist != string.Empty)
                {
                    Console.WriteLine("Run list: " + options.runlist);
                    using (StreamReader rdr = new StreamReader(options.runlist))
                    {
                        // NOTE: We can't use rdr.EndOfStream because it's
                        // not present in .NET 1.x.
                        string line = rdr.ReadLine();
                        while (line != null)
                        {
                            if (line[0] != '#')
                            {
                                nameFilter.Add(line);
                            }
                            line = rdr.ReadLine();
                        }
                    }
                    testFilter = nameFilter;
                }

                if (options.include != null && options.include != string.Empty)
                {
                    TestFilter includeFilter = new CategoryExpression(options.include).Filter;
                    Console.WriteLine("Included categories: " + includeFilter.ToString());

                    if (testFilter.IsEmpty)
                    {
                        testFilter = includeFilter;
                    }
                    else
                    {
                        testFilter = new AndFilter(testFilter, includeFilter);
                    }
                }

                if (options.exclude != null && options.exclude != string.Empty)
                {
                    TestFilter excludeFilter = new NotFilter(new CategoryExpression(options.exclude).Filter);
                    Console.WriteLine("Excluded categories: " + excludeFilter.ToString());

                    if (testFilter.IsEmpty)
                    {
                        testFilter = excludeFilter;
                    }
                    else if (testFilter is AndFilter)
                    {
                        ((AndFilter)testFilter).Add(excludeFilter);
                    }
                    else
                    {
                        testFilter = new AndFilter(testFilter, excludeFilter);
                    }
                }

                if (testFilter is NotFilter)
                {
                    ((NotFilter)testFilter).TopLevel = true;
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, testFilter, false, LoggingThreshold.Off);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }
                    if (redirectError)
                    {
                        errorWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                int returnCode = UNEXPECTED_ERROR;

                if (result != null)
                {
                    string           xmlOutput = CreateXmlOutput(result);
                    ResultSummarizer summary   = new ResultSummarizer(result);

                    if (options.xmlConsole)
                    {
                        Console.WriteLine(xmlOutput);
                    }
                    else
                    {
                        WriteSummaryReport(summary);
                        if (summary.ErrorsAndFailures > 0 || result.IsError || result.IsFailure)
                        {
                            if (options.stoponerror)
                            {
                                Console.WriteLine("Test run was stopped after first error, as requested.");
                                Console.WriteLine();
                            }

                            WriteErrorsAndFailuresReport(result);
                        }
                        if (summary.TestsNotRun > 0)
                        {
                            WriteNotRunReport(result);
                        }

                        if (!options.noresult)
                        {
                            // Write xml output here
                            string xmlResultFile = options.result == null || options.result == string.Empty
                                ? "TestResult.xml" : options.result;

                            using (StreamWriter writer = new StreamWriter(Path.Combine(workDir, xmlResultFile)))
                            {
                                writer.Write(xmlOutput);
                            }
                        }
                    }

                    returnCode = summary.ErrorsAndFailures;
                }

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    returnCode = UNEXPECTED_ERROR;
                }

                return(returnCode);
            }
        }
예제 #22
0
        public int Execute(ConsoleOptions options)
        {
            this.workDir = options.work;
            if (workDir == null || workDir == string.Empty)
            {
                workDir = Environment.CurrentDirectory;
            }
            else
            {
                workDir = Path.GetFullPath(workDir);
                if (!Directory.Exists(workDir))
                {
                    Directory.CreateDirectory(workDir);
                }
            }

            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = options.output != null && options.output != string.Empty;

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(Path.Combine(workDir, options.output));
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = options.err != null && options.err != string.Empty;

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(Path.Combine(workDir, options.err));
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TestPackage package = MakeTestPackage(options);

            ProcessModel processModel = package.Settings.Contains("ProcessModel")
                ? (ProcessModel)package.Settings["ProcessModel"]
                : ProcessModel.Default;

            RuntimeFramework framework = package.Settings.Contains("RuntimeFramework")
                ? (RuntimeFramework)package.Settings["RuntimeFramework"]
                : RuntimeFramework.CurrentFramework;

#if CLR_2_0 || CLR_4_0
#else
            if (processModel != ProcessModel.Default && processModel != ProcessModel.Single)
            {
                Console.WriteLine("Warning: Ignoring project setting 'processModel={0}'", processModel);
            }

            if (!RuntimeFramework.CurrentFramework.Supports(framework))
            {
                Console.WriteLine("Warning: Ignoring project setting 'runtimeFramework={0}'", framework);
            }
#endif

            using (TestRunner testRunner = new DefaultTestRunnerFactory().MakeTestRunner(package))
            {
                testRunner.Load(package);

                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(FIXTURE_NOT_FOUND);
                }

                EventCollector collector = new EventCollector(options, outWriter, errorWriter);

                TestFilter testFilter;

                if (!CreateTestFilter(options, out testFilter))
                {
                    return(INVALID_ARG);
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, testFilter, false, LoggingThreshold.Off);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }

                    if (redirectError)
                    {
                        errorWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                int returnCode = UNEXPECTED_ERROR;

                if (result != null)
                {
                    string           xmlOutput = CreateXmlOutput(result);
                    ResultSummarizer summary   = new ResultSummarizer(result);

                    if (options.xmlConsole)
                    {
                        Console.WriteLine(xmlOutput);
                    }
                    else
                    {
                        WriteSummaryReport(summary);

                        bool hasErrors = summary.Errors > 0 || summary.Failures > 0 || result.IsError || result.IsFailure;

                        if (options.stoponerror && (hasErrors || summary.NotRunnable > 0))
                        {
                            Console.WriteLine("Test run was stopped after first error, as requested.");
                            Console.WriteLine();
                        }

                        if (hasErrors)
                        {
                            WriteErrorsAndFailuresReport(result);
                        }

                        if (summary.TestsNotRun > 0)
                        {
                            WriteNotRunReport(result);
                        }

                        if (!options.noresult)
                        {
                            // Write xml output here
                            string xmlResultFile = options.result == null || options.result == string.Empty
                                ? "TestResult.xml" : options.result;

                            using (StreamWriter writer = new StreamWriter(Path.Combine(workDir, xmlResultFile)))
                            {
                                writer.Write(xmlOutput);
                            }
                        }
                    }

                    returnCode = summary.Errors + summary.Failures + summary.NotRunnable;
                }

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    returnCode = UNEXPECTED_ERROR;
                }

                return(returnCode);
            }
        }