public UnitTestResult Run(TestContext testContext) { testContext.Monitor.BeginTest(this); UnitTestResult res = null; object ctx = testContext.ContextData; try { Status = TestStatus.Running; res = OnRun(testContext); } catch (global::NUnit.Framework.SuccessException) { res = UnitTestResult.CreateSuccess(); } catch (global::NUnit.Framework.IgnoreException ex) { res = UnitTestResult.CreateIgnored(ex.Message); } catch (global::NUnit.Framework.InconclusiveException ex) { res = UnitTestResult.CreateInconclusive(ex.Message); } catch (Exception ex) { res = UnitTestResult.CreateFailure(ex); } finally { Status = TestStatus.Ready; testContext.Monitor.EndTest(this, res); } RegisterResult(testContext, res); testContext.ContextData = ctx; return(res); }
UnitTestResult RunWithConsoleRunner(ProcessExecutionCommand cmd, UnitTest test, string suiteName, string pathName, string testName, TestContext testContext) { var outFile = Path.GetTempFileName(); LocalConsole cons = new LocalConsole(); try { MonoDevelop.NUnit.External.TcpTestListener tcpListener = null; LocalTestMonitor localMonitor = new LocalTestMonitor(testContext, test, suiteName, testName != null); if (!string.IsNullOrEmpty(cmd.Arguments)) { cmd.Arguments += " "; } cmd.Arguments += "\"-xml=" + outFile + "\" " + AssemblyPath; bool automaticUpdates = cmd.Command.Contains("GuiUnit") || (cmd.Command.Contains("mdtool.exe") && cmd.Arguments.Contains("run-md-tests")); if (!string.IsNullOrEmpty(pathName)) { cmd.Arguments += " -run=" + pathName; } if (automaticUpdates) { tcpListener = new MonoDevelop.NUnit.External.TcpTestListener(localMonitor, suiteName); cmd.Arguments += " -port=" + tcpListener.Port; } // Note that we always dispose the tcp listener as we don't want it listening // forever if the test runner does not try to connect to it using (tcpListener) { var p = testContext.ExecutionContext.Execute(cmd, cons); testContext.Monitor.CancelRequested += p.Cancel; if (testContext.Monitor.IsCancelRequested) { p.Cancel(); } p.WaitForCompleted(); if (new FileInfo(outFile).Length == 0) { throw new Exception("Command failed"); } } // mdtool.exe does not necessarily guarantee we get automatic updates. It just guarantees // that if guiunit is being used then it will give us updates. If you have a regular test // assembly compiled against nunit.framework.dll if (automaticUpdates && tcpListener.HasReceivedConnection) { if (testName != null) { return(localMonitor.SingleTestResult); } return(test.GetLastResult()); } XDocument doc = XDocument.Load(outFile); if (doc.Root != null) { var root = doc.Root.Elements("test-suite").FirstOrDefault(); if (root != null) { cons.SetDone(); var ot = cons.Out.ReadToEnd(); var et = cons.Error.ReadToEnd(); testContext.Monitor.WriteGlobalLog(ot); if (!string.IsNullOrEmpty(et)) { testContext.Monitor.WriteGlobalLog("ERROR:\n"); testContext.Monitor.WriteGlobalLog(et); } bool macunitStyle = doc.Root.Element("environment") != null && doc.Root.Element("environment").Attribute("macunit-version") != null; var result = ReportXmlResult(localMonitor, root, "", macunitStyle); if (testName != null) { result = localMonitor.SingleTestResult; } return(result); } } throw new Exception("Test results could not be parsed."); } catch (Exception ex) { cons.SetDone(); var ot = cons.Out.ReadToEnd(); var et = cons.Error.ReadToEnd(); testContext.Monitor.WriteGlobalLog(ot); if (!string.IsNullOrEmpty(et)) { testContext.Monitor.WriteGlobalLog("ERROR:\n"); testContext.Monitor.WriteGlobalLog(et); } testContext.Monitor.ReportRuntimeError("Test execution failed.\n" + ot + "\n" + et, ex); return(UnitTestResult.CreateIgnored("Test execution failed")); } finally { File.Delete(outFile); } }
UnitTestResult RunWithConsoleRunner(ProcessExecutionCommand cmd, UnitTest test, string suiteName, string pathName, string testName, TestContext testContext) { var outFile = Path.GetTempFileName(); LocalConsole cons = new LocalConsole(); try { if (!string.IsNullOrEmpty(cmd.Arguments)) { cmd.Arguments += " "; } cmd.Arguments += "\"-xml=" + outFile + "\" " + AssemblyPath; if (!string.IsNullOrEmpty(testName)) { cmd.Arguments += " -run=" + suiteName + "." + testName; } else if (!string.IsNullOrEmpty(suiteName)) { cmd.Arguments += " -run=" + suiteName; } var p = testContext.ExecutionContext.Execute(cmd, cons); testContext.Monitor.CancelRequested += p.Cancel; if (testContext.Monitor.IsCancelRequested) { p.Cancel(); } p.WaitForCompleted(); if (new FileInfo(outFile).Length == 0) { throw new Exception("Command failed"); } LocalTestMonitor localMonitor = new LocalTestMonitor(testContext, test, suiteName, testName != null); XDocument doc = XDocument.Load(outFile); if (doc.Root != null) { var root = doc.Root.Elements("test-suite").FirstOrDefault(); if (root != null) { cons.SetDone(); var ot = cons.Out.ReadToEnd(); var et = cons.Error.ReadToEnd(); testContext.Monitor.WriteGlobalLog(ot); if (!string.IsNullOrEmpty(et)) { testContext.Monitor.WriteGlobalLog("ERROR:\n"); testContext.Monitor.WriteGlobalLog(et); } return(ReportXmlResult(localMonitor, root, "")); } } throw new Exception("Test results could not be parsed."); } catch (Exception ex) { cons.SetDone(); var ot = cons.Out.ReadToEnd(); var et = cons.Error.ReadToEnd(); testContext.Monitor.WriteGlobalLog(ot); if (!string.IsNullOrEmpty(et)) { testContext.Monitor.WriteGlobalLog("ERROR:\n"); testContext.Monitor.WriteGlobalLog(et); } testContext.Monitor.ReportRuntimeError("Test execution failed.\n" + ot + "\n" + et, ex); return(UnitTestResult.CreateIgnored("Test execution failed")); } finally { File.Delete(outFile); } }
UnitTestResult RunWithConsoleRunner(ProcessExecutionCommand cmd, UnitTest test, string suiteName, string pathName, string testName, TestContext testContext) { var outFile = Path.GetTempFileName(); LocalConsole cons = new LocalConsole(); try { LocalTestMonitor localMonitor = new LocalTestMonitor(testContext, test, suiteName, testName != null); if (!string.IsNullOrEmpty(cmd.Arguments)) { cmd.Arguments += " "; } cmd.Arguments += "\"-xml=" + outFile + "\" " + AssemblyPath; bool automaticUpdates = cmd.Command.Contains("GuiUnit") || (cmd.Command.Contains("mdtool.exe") && cmd.Arguments.Contains("run-md-tests")); if (!string.IsNullOrEmpty(testName)) { cmd.Arguments += " -run=" + suiteName + "." + testName; } else if (!string.IsNullOrEmpty(suiteName)) { cmd.Arguments += " -run=" + suiteName; } if (automaticUpdates) { var tcpListener = new MonoDevelop.NUnit.External.TcpTestListener(localMonitor, suiteName); cmd.Arguments += " -port=" + tcpListener.Port; } var p = testContext.ExecutionContext.Execute(cmd, cons); testContext.Monitor.CancelRequested += p.Cancel; if (testContext.Monitor.IsCancelRequested) { p.Cancel(); } p.WaitForCompleted(); if (new FileInfo(outFile).Length == 0) { throw new Exception("Command failed"); } if (automaticUpdates) { if (testName != null) { return(localMonitor.SingleTestResult); } return(test.GetLastResult()); } XDocument doc = XDocument.Load(outFile); if (doc.Root != null) { var root = doc.Root.Elements("test-suite").FirstOrDefault(); if (root != null) { cons.SetDone(); var ot = cons.Out.ReadToEnd(); var et = cons.Error.ReadToEnd(); testContext.Monitor.WriteGlobalLog(ot); if (!string.IsNullOrEmpty(et)) { testContext.Monitor.WriteGlobalLog("ERROR:\n"); testContext.Monitor.WriteGlobalLog(et); } bool macunitStyle = doc.Root.Element("environment") != null && doc.Root.Element("environment").Attribute("macunit-version") != null; var result = ReportXmlResult(localMonitor, root, "", macunitStyle); if (testName != null) { result = localMonitor.SingleTestResult; } return(result); } } throw new Exception("Test results could not be parsed."); } catch (Exception ex) { cons.SetDone(); var ot = cons.Out.ReadToEnd(); var et = cons.Error.ReadToEnd(); testContext.Monitor.WriteGlobalLog(ot); if (!string.IsNullOrEmpty(et)) { testContext.Monitor.WriteGlobalLog("ERROR:\n"); testContext.Monitor.WriteGlobalLog(et); } testContext.Monitor.ReportRuntimeError("Test execution failed.\n" + ot + "\n" + et, ex); return(UnitTestResult.CreateIgnored("Test execution failed")); } finally { File.Delete(outFile); } }