/// <summary> /// Runs the requested test and return result XML. /// </summary> /// <param name="testName"></param> /// <returns>Result XML document</returns> private string RunTest(string testName, bool explicitRun) { // Execute the give test. SimpleNameFilter testFilter = new SimpleNameFilter(); testFilter.Add(testName); TestSuite testSuite = new TestBuilder().Build(assemblyName, true); //TestResult result = testSuite.Run(this, testFilter); TestResult result = null; Queue<ITest> testQueue = new Queue<ITest>(); testQueue.Enqueue(testSuite); while (testQueue.Count > 0) { ITest test = testQueue.Dequeue(); if (test.Tests != null) { foreach (ITest childTest in test.Tests) { testQueue.Enqueue(childTest); } } else { if (test.TestName.FullName.Equals(testName)) { if (!explicitRun && test.RunState == RunState.Explicit) { result = new TestResult(test.TestName); } else { result = ((Test)test).Run(this, null); } } } } // Trace error stack trace. if (result.StackTrace != null && result.StackTrace.Length > 0) { Trace.TraceError(result.StackTrace); } // Serialize result to XML. StringBuilder builder = new StringBuilder(); new XmlResultWriter(new StringWriter(builder)).SaveTestResult(result); return builder.ToString(); }
static void Main(string[] args) { try { CoreExtensions.Host.InitializeService(); string assemblyName = ""; if (args.Length == 2) { assemblyName = args[1]; } else { Console.WriteLine("usage: VisualNunitRunner.exe [run|list|serve] file"); return; } if (args.Length == 2 && args[0] == "run") { // Run tests according to console input. ConsoleTraceListener consoleListener = new ConsoleTraceListener(); Trace.Listeners.Add(consoleListener); ConsoleTestRunListener runnerListener = new ConsoleTestRunListener(); SimpleNameFilter testFilter = new SimpleNameFilter(); testFilter.Add(Console.ReadLine()); TestSuite testSuite = new TestBuilder().Build(assemblyName, true); TestResult result = testSuite.Run(runnerListener, testFilter); if (result.StackTrace != null && result.StackTrace.Length > 0) { Trace.TraceError(result.StackTrace); } Console.WriteLine("beginning-of-test-result-xml"); StringBuilder builder = new StringBuilder(); new XmlResultWriter(new StringWriter(builder)).SaveTestResult(result); Console.WriteLine(builder.ToString()); } else if (args.Length == 2 && args[0] == "list") { // List tests to console output. ConsoleTraceListener consoleListener = new ConsoleTraceListener(); Trace.Listeners.Add(consoleListener); TestSuite testSuite = new TestBuilder().Build(assemblyName, true); Queue<ITest> testQueue = new Queue<ITest>(); testQueue.Enqueue(testSuite); while (testQueue.Count > 0) { ITest test = testQueue.Dequeue(); if (test.Tests != null) { foreach (ITest childTest in test.Tests) { testQueue.Enqueue(childTest); } } else { Console.WriteLine(test.TestName.FullName); } } } else if (args.Length == 2 && args[0] == "serve") { // Run in service mode to serve visual studio via named pipes. RunnerServer runnerServer = new RunnerServer(assemblyName); while (runnerServer.IsAlive) { Thread.Sleep(10); } System.Environment.Exit(0); } } catch (Exception e) { Trace.TraceError("VisualNunitRunner failed: "+e); } }
/// <summary> /// List the unit tests available to the pipe as single message. /// </summary> private void ListTests() { try { // Build test suite from the given assembly. TestSuite testSuite = new TestBuilder().Build(assemblyName, true); // Recursively browse tests and concatenate full test names to string in separate lines. Queue<ITest> testQueue = new Queue<ITest>(); testQueue.Enqueue(testSuite); String testNames = "Tests:"; while (testQueue.Count > 0) { ITest test = testQueue.Dequeue(); if (test.Tests != null) { foreach (ITest childTest in test.Tests) { testQueue.Enqueue(childTest); } } else { if (testNames.Length > 0) { testNames += "\n"; } testNames += test.TestName.FullName; } } // Write test names to the pipe. byte[] testNameBytes = Encoding.UTF8.GetBytes(testNames); pipe.Write(testNameBytes, 0, testNameBytes.Length); pipe.Flush(); } catch (Exception) { // Write empty test names to the pipe. byte[] testNameBytes = Encoding.UTF8.GetBytes("Tests:"); pipe.Write(testNameBytes, 0, testNameBytes.Length); pipe.Flush(); } }