Пример #1
0
        public static int Main(string[] args)
        {
            ExtendedConsoleOptions options = new ExtendedConsoleOptions(args);

            // Create SettingsService early so we know the trace level right at the start
            SettingsService    settingsService = new SettingsService();
            InternalTraceLevel level           = (InternalTraceLevel)settingsService.GetSetting("Options.InternalTraceLevel", InternalTraceLevel.Default);

            if (options.trace != InternalTraceLevel.Default)
            {
                level = options.trace;
            }

            InternalTrace.Initialize("nunit-console_%p.log", level);

            log.Info("NUnit-console.exe starting");

            if (!options.nologo)
            {
                WriteCopyright();
            }

            if (options.help)
            {
                options.Help();
                return(ConsoleUi.OK);
            }

            if (options.NoArgs)
            {
                Console.Error.WriteLine("fatal error: no inputs specified");
                options.Help();
                return(ConsoleUi.OK);
            }

            if (!options.Validate())
            {
                foreach (string arg in options.InvalidArguments)
                {
                    Console.Error.WriteLine("fatal error: invalid argument: {0}", arg);
                }
                options.Help();
                return(ConsoleUi.INVALID_ARG);
            }

            // Add Standard Services to ServiceManager
            ServiceManager.Services.AddService(settingsService);
            ServiceManager.Services.AddService(new DomainManager());
            //ServiceManager.Services.AddService( new RecentFilesService() );
            ServiceManager.Services.AddService(new ProjectService());
            //ServiceManager.Services.AddService( new TestLoader() );
            ServiceManager.Services.AddService(new AddinRegistry());
            ServiceManager.Services.AddService(new AddinManager());
            ServiceManager.Services.AddService(new TestAgency());

            // Initialize Services
            ServiceManager.Services.InitializeServices();

            foreach (string parm in options.Parameters)
            {
                if (!Services.ProjectService.CanLoadProject(parm) && !PathUtils.IsAssemblyFileType(parm))
                {
                    Console.WriteLine("File type not known: {0}", parm);
                    return(ConsoleUi.INVALID_ARG);
                }
            }

            try
            {
                ExtendedConsoleUi consoleUi = new ExtendedConsoleUi();
                return(consoleUi.Execute(options));
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                return(ConsoleUi.FILE_NOT_FOUND);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled Exception:\n{0}", ex.ToString());
                return(ConsoleUi.UNEXPECTED_ERROR);
            }
            finally
            {
                if (options.wait)
                {
                    Console.Out.WriteLine("\nHit <enter> key to continue");
                    Console.ReadLine();
                }

                log.Info("NUnit-console.exe terminating");
            }
        }
Пример #2
0
		public static int Main(string[] args)
		{
			ExtendedConsoleOptions options = new ExtendedConsoleOptions(args);

            // Create SettingsService early so we know the trace level right at the start
            SettingsService settingsService = new SettingsService();
            InternalTraceLevel level = (InternalTraceLevel)settingsService.GetSetting("Options.InternalTraceLevel", InternalTraceLevel.Default);
            if (options.trace != InternalTraceLevel.Default)
                level = options.trace;

            InternalTrace.Initialize("nunit-console_%p.log", level);
            
            log.Info("NUnit-console.exe starting");
            
			if(!options.nologo)
				WriteCopyright();

			if(options.help)
			{
				options.Help();
				return ConsoleUi.OK;
			}
			
			if(options.NoArgs) 
			{
				Console.Error.WriteLine("fatal error: no inputs specified");
				options.Help();
				return ConsoleUi.OK;
			}
			
			if(!options.Validate())
			{
				foreach( string arg in options.InvalidArguments )
					Console.Error.WriteLine("fatal error: invalid argument: {0}", arg );
				options.Help();
				return ConsoleUi.INVALID_ARG;
			}

			// Add Standard Services to ServiceManager
			ServiceManager.Services.AddService( settingsService );
			ServiceManager.Services.AddService( new DomainManager() );
			//ServiceManager.Services.AddService( new RecentFilesService() );
			ServiceManager.Services.AddService( new ProjectService() );
			//ServiceManager.Services.AddService( new TestLoader() );
			ServiceManager.Services.AddService( new AddinRegistry() );
			ServiceManager.Services.AddService( new AddinManager() );
      ServiceManager.Services.AddService( new TestAgency() );

			// Initialize Services
			ServiceManager.Services.InitializeServices();
			
			foreach (string parm in options.Parameters)
			{
				if (!Services.ProjectService.CanLoadProject(parm) && !PathUtils.IsAssemblyFileType(parm))
				{
					Console.WriteLine("File type not known: {0}", parm);
					return ConsoleUi.INVALID_ARG;
				}
			}
			
			try
			{
				ExtendedConsoleUi consoleUi = new ExtendedConsoleUi();
				return consoleUi.Execute( options );
			}
			catch( FileNotFoundException ex )
			{
				Console.WriteLine( ex.Message );
				return ConsoleUi.FILE_NOT_FOUND;
			}
			catch( Exception ex )
			{
				Console.WriteLine( "Unhandled Exception:\n{0}", ex.ToString() );
				return ConsoleUi.UNEXPECTED_ERROR;
			}
			finally
			{
				if(options.wait)
				{
					Console.Out.WriteLine("\nHit <enter> key to continue");
					Console.ReadLine();
				}

				log.Info( "NUnit-console.exe terminating" );
			}

		}
        public int Execute(ExtendedConsoleOptions 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;
            }

            TextWriter testResultWriter = null;

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

            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);
                }

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

                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();
                    }

                    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);

                        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);
            }
        }
Пример #4
0
		public int Execute( ExtendedConsoleOptions 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;
			}
			
			TextWriter testResultWriter = null;
			if ( options.IsResults )
			{
				testResultWriter = new StreamWriter ( options.results, false, Encoding.UTF8 );
				((StreamWriter)testResultWriter).AutoFlush = true;
			}
            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;
				}

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

				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();
					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 || 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;
			}
		}
        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();
            }
        }
Пример #6
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();
            }
        }
Пример #7
0
        public int Execute( ExtendedConsoleOptions 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;
            }

            TextWriter testResultWriter = null;
            if ( options.UsePipe )
            {
                var namedPipe = new NamedPipeClientStream(".", options.pipe, PipeDirection.Out, PipeOptions.WriteThrough);
                namedPipe.Connect();
                testResultWriter = new StreamWriter ( namedPipe, Encoding.UTF8 );
                ((StreamWriter)testResultWriter).AutoFlush = true;
            }

            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;
                }

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

                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();

                    if ( testResultWriter != null )
                        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);

                        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;
            }
        }
Пример #8
0
        public static int Main(string[] args)
        {
            log.Info("NUnit-console.exe starting");

            ExtendedConsoleOptions options = new ExtendedConsoleOptions(args);

            if (!options.nologo)
            {
                WriteCopyright();
            }

            if (options.help)
            {
                options.Help();
                return(ConsoleUi.OK);
            }

            if (options.NoArgs)
            {
                Console.Error.WriteLine("fatal error: no inputs specified");
                options.Help();
                return(ConsoleUi.OK);
            }

            if (!options.Validate())
            {
                foreach (string arg in options.InvalidArguments)
                {
                    Console.Error.WriteLine("fatal error: invalid argument: {0}", arg);
                }
                options.Help();
                return(ConsoleUi.INVALID_ARG);
            }

            // Add Standard Services to ServiceManager
            ServiceManager.Services.AddService(new SettingsService());
            ServiceManager.Services.AddService(new DomainManager());
            //ServiceManager.Services.AddService( new RecentFilesService() );
            ServiceManager.Services.AddService(new ProjectService());
            //ServiceManager.Services.AddService( new TestLoader() );
            ServiceManager.Services.AddService(new AddinRegistry());
            ServiceManager.Services.AddService(new AddinManager());
            // Hack: Resolves conflict with gui testagency when running
            // console tests under the gui.
            if (!AppDomain.CurrentDomain.FriendlyName.StartsWith("test-domain-"))
            {
                ServiceManager.Services.AddService(new TestAgency());
            }

            // Initialize Services
            ServiceManager.Services.InitializeServices();

            foreach (string parm in options.Parameters)
            {
                if (!Services.ProjectService.CanLoadProject(parm) && !PathUtils.IsAssemblyFileType(parm))
                {
                    Console.WriteLine("File type not known: {0}", parm);
                    return(ConsoleUi.INVALID_ARG);
                }
            }

            try
            {
                ExtendedConsoleUi consoleUi = new ExtendedConsoleUi();
                return(consoleUi.Execute(options));
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                return(ConsoleUi.FILE_NOT_FOUND);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled Exception:\n{0}", ex.ToString());
                return(ConsoleUi.UNEXPECTED_ERROR);
            }
            finally
            {
                if (options.wait)
                {
                    Console.Out.WriteLine("\nHit <enter> key to continue");
                    Console.ReadLine();
                }

                log.Info("NUnit-console.exe terminating");
            }
        }
Пример #9
0
        public static int Main(string[] args)
        {
            ExtendedConsoleOptions options = new ExtendedConsoleOptions(args);

            if (!options.nologo)
            {
                WriteCopyright();
            }

            if (options.help)
            {
                options.Help();
                return(0);
            }

            if (options.NoArgs)
            {
                Console.Error.WriteLine("fatal error: no inputs specified");
                options.Help();
                return(0);
            }

            if (!options.Validate())
            {
                Console.Error.WriteLine("fatal error: invalid arguments");
                options.Help();
                return(2);
            }


            // Add Standard Services to ServiceManager
            ServiceManager.Services.AddService(new SettingsService());
            ServiceManager.Services.AddService(new DomainManager());
            //ServiceManager.Services.AddService( new RecentFilesService() );
            //ServiceManager.Services.AddService( new TestLoader() );
            ServiceManager.Services.AddService(new AddinRegistry());
            ServiceManager.Services.AddService(new AddinManager());

            // Initialize Services
            ServiceManager.Services.InitializeServices();

            try
            {
                ExtendedConsoleUi consoleUi = new ExtendedConsoleUi();
                return(consoleUi.Execute(options));
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                return(2);
            }
            catch (BadImageFormatException ex)
            {
                Console.WriteLine(ex.Message);
                return(2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled Exception:\n{0}", ex.ToString());
                return(2);
            }
            finally
            {
                if (options.wait)
                {
                    Console.Out.WriteLine("\nHit <enter> key to continue");
                    Console.ReadLine();
                }
            }
        }
Пример #10
0
        public int Execute(ExtendedConsoleOptions options)
        {
            XmlTextReader transformReader = GetTransformReader(options);

            if (transformReader == null)
            {
                return(3);
            }

            TextWriter outWriter = Console.Out;

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

            TextWriter errorWriter = Console.Error;

            if (options.isErr)
            {
                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;
            }

            TestRunner testRunner = MakeRunnerFromCommandLine(options);

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

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

                TestFilter catFilter = TestFilter.Empty;

                if (options.HasInclude)
                {
                    Console.WriteLine("Included categories: " + options.include);
                    catFilter = new CategoryFilter(options.IncludedCategories);
                }

                if (options.HasExclude)
                {
                    Console.WriteLine("Excluded categories: " + options.exclude);
                    TestFilter excludeFilter = new NotFilter(new CategoryFilter(options.ExcludedCategories));
                    catFilter = AndFilter(catFilter, excludeFilter);
                }

                if (options.HasNamespaceFilter)
                {
                    Console.WriteLine("Namespace filter: " + options.namespaceFilter);
                    TestFilter namespaceFilter = GetNamespaceFilter(testRunner, options.namespaceFilter);
                    catFilter = AndFilter(catFilter, namespaceFilter);
                }

                if (options.HasTestMethodName || options.IsFixture)
                {
                    SimpleNameFilter nameFilter = new SimpleNameFilter();
                    if (options.HasTestMethodName)
                    {
                        nameFilter.Add(options.testMethodName);
                    }
                    else
                    {
                        nameFilter.Add(options.fixture);
                    }
                    catFilter = AndFilter(catFilter, nameFilter);
                }

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

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

                    if (options.isOut)
                    {
                        outWriter.Close();
                    }
                    if (options.isErr)
                    {
                        errorWriter.Close();
                    }
                    if (options.IsResults)
                    {
                        testResultWriter.Close();
                    }

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

                Console.WriteLine();

                string xmlOutput = CreateXmlOutput(result);

                if (options.xmlConsole)
                {
                    Console.WriteLine(xmlOutput);
                }
                else
                {
                    try
                    {
                        //CreateSummaryDocument(xmlOutput, transformReader );
                        XmlResultTransform xform = new XmlResultTransform(transformReader);
                        xform.Transform(new StringReader(xmlOutput), Console.Out);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: {0}", ex.Message);
                        return(3);
                    }
                }

                // Write xml output here
                string xmlResultFile = options.IsXml ? options.xml : "TestResult.xml";

                using (StreamWriter writer = new StreamWriter(xmlResultFile))
                {
                    writer.Write(xmlOutput);
                }

                //if ( testRunner != null )
                //	testRunner.Unload();

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    return(2);
                }

                return(result.IsFailure ? 1 : 0);
            }
            finally
            {
                testRunner.Unload();
            }
        }
        public int Execute(ExtendedConsoleOptions 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;
            }

            TextWriter testResultWriter = null;

            if (options.IsResults)
            {
                testResultWriter = new StreamWriter(options.results, false, Encoding.UTF8);
                ((StreamWriter)testResultWriter).AutoFlush = true;
            }
            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);
                }

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

                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();
                    }
                    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 || 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);
            }
        }