/// <summary> /// runs the given test /// </summary> /// <param name="testPath"></param> /// <param name="errorReason"></param> /// <param name="runCancelled">cancellation delegate, holds the function that checks cancellation</param> /// <returns></returns> public TestRunResults RunTest(string testPath, ref string errorReason, RunCancelledDelegate runCancelled) { TestRunResults runDesc = new TestRunResults(); ConsoleWriter.ActiveTestRun = runDesc; ConsoleWriter.WriteLine(DateTime.Now.ToString(Launcher.DateFormat) + " Running: " + testPath); runDesc.ReportLocation = Helper.CreateTempDir(); runDesc.ErrorDesc = errorReason; runDesc.TestPath = testPath; runDesc.TestState = TestState.Unknown; if (!Helper.IsServiceTestInstalled()) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = string.Format(Resources.LauncherStNotInstalled, System.Environment.MachineName); ConsoleWriter.WriteErrLine(runDesc.ErrorDesc); Environment.ExitCode = (int)Launcher.ExitCodeEnum.Failed; return(runDesc); } _runCancelled = runCancelled; if (!_stCanRun) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = Resources.STExecuterNotFound; return(runDesc); } string fileName = Path.Combine(_stExecuterPath, STRunnerName); if (!File.Exists(fileName)) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = Resources.STExecuterNotFound; ConsoleWriter.WriteErrLine(Resources.STExecuterNotFound); return(runDesc); } Stopwatch s = Stopwatch.StartNew(); runDesc.TestState = TestState.Running; if (!ExecuteProcess(fileName, String.Format("{0} \"{1}\" {2} \"{3}\" ", STRunnerTestArg, testPath, STRunnerReportArg, runDesc.ReportLocation), ref errorReason)) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = errorReason; runDesc.Runtime = s.Elapsed; return(runDesc); } else { runDesc.ReportLocation = Path.Combine(runDesc.ReportLocation, "Report"); } runDesc.Runtime = s.Elapsed; return(runDesc); }
/// <summary> /// runs the given test /// </summary> /// <param name="testinf"></param> /// <param name="errorReason"></param> /// <param name="runCancelled">cancellation delegate, holds the function that checks cancellation</param> /// <returns></returns> public TestRunResults RunTest(TestInfo testinf, ref string errorReason, RunCancelledDelegate runCancelled) { TestRunResults runDesc = new TestRunResults(); ConsoleWriter.ActiveTestRun = runDesc; ConsoleWriter.WriteLine(DateTime.Now.ToString(Launcher.DateFormat) + " Running: " + testinf.TestPath); runDesc.ReportLocation = Helper.CreateTempDir(); runDesc.ErrorDesc = errorReason; runDesc.TestPath = testinf.TestPath; runDesc.TestState = TestState.Unknown; if (!Helper.IsServiceTestInstalled()) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = string.Format(Resources.LauncherStNotInstalled, System.Environment.MachineName); ConsoleWriter.WriteErrLine(runDesc.ErrorDesc); Environment.ExitCode = (int)Launcher.ExitCodeEnum.Failed; return(runDesc); } _runCancelled = runCancelled; if (!_stCanRun) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = Resources.STExecuterNotFound; return(runDesc); } string fileName = Path.Combine(_stExecuterPath, STRunnerName); if (!File.Exists(fileName)) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = Resources.STExecuterNotFound; ConsoleWriter.WriteErrLine(Resources.STExecuterNotFound); return(runDesc); } //write the input parameter xml file for the API test string paramsFile = Path.GetTempFileName(); string paramFileContent = testinf.GenerateAPITestXmlForTest(); string argumentString = ""; if (!string.IsNullOrWhiteSpace(paramFileContent)) { File.WriteAllText(paramsFile, paramFileContent); argumentString = String.Format("{0} \"{1}\" {2} \"{3}\" {4} \"{5}\"", STRunnerTestArg, testinf.TestPath, STRunnerReportArg, runDesc.ReportLocation, STRunnerInputParamsArg, paramsFile); } else { argumentString = String.Format("{0} \"{1}\" {2} \"{3}\"", STRunnerTestArg, testinf.TestPath, STRunnerReportArg, runDesc.ReportLocation); } Stopwatch s = Stopwatch.StartNew(); runDesc.TestState = TestState.Running; if (!ExecuteProcess(fileName, argumentString, ref errorReason)) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = errorReason; } else { runDesc.ReportLocation = Path.Combine(runDesc.ReportLocation, "Report"); if (!File.Exists(Path.Combine(runDesc.ReportLocation, "Results.xml"))) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = "No Results.xml file found"; } } runDesc.Runtime = s.Elapsed; return(runDesc); }
/// <summary> /// runs the given test /// </summary> /// <param name="testinf"></param> /// <param name="errorReason"></param> /// <param name="runCancelled">cancellation delegate, holds the function that checks cancellation</param> /// <returns></returns> public TestRunResults RunTest(TestInfo testinf, ref string errorReason, RunCancelledDelegate runCancelled) { TestRunResults runDesc = new TestRunResults(); ConsoleWriter.ActiveTestRun = runDesc; ConsoleWriter.WriteLine(DateTime.Now.ToString(Launcher.DateFormat) + " Running: " + testinf.TestPath); runDesc.TestPath = testinf.TestPath; // default report location is the test path runDesc.ReportLocation = testinf.TestPath; // check if the report path has been defined if (!String.IsNullOrEmpty(testinf.ReportPath)) { if (!Helper.TrySetTestReportPath(runDesc, testinf, ref errorReason)) { return(runDesc); } } runDesc.ErrorDesc = errorReason; runDesc.TestState = TestState.Unknown; if (!Helper.IsServiceTestInstalled()) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = string.Format(Resources.LauncherStNotInstalled, System.Environment.MachineName); ConsoleWriter.WriteErrLine(runDesc.ErrorDesc); Environment.ExitCode = (int)Launcher.ExitCodeEnum.Failed; return(runDesc); } _runCancelled = runCancelled; if (!_stCanRun) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = Resources.STExecuterNotFound; return(runDesc); } string fileName = Path.Combine(_stExecuterPath, STRunnerName); if (!File.Exists(fileName)) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = Resources.STExecuterNotFound; ConsoleWriter.WriteErrLine(Resources.STExecuterNotFound); return(runDesc); } //write the input parameter xml file for the API test string paramFileName = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 10); string tempPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestParams"); Directory.CreateDirectory(tempPath); string paramsFilePath = Path.Combine(tempPath, "params" + paramFileName + ".xml"); string paramFileContent = testinf.GenerateAPITestXmlForTest(); string argumentString = ""; if (!string.IsNullOrWhiteSpace(paramFileContent)) { File.WriteAllText(paramsFilePath, paramFileContent); argumentString = String.Format("{0} \"{1}\" {2} \"{3}\" {4} \"{5}\"", STRunnerTestArg, testinf.TestPath, STRunnerReportArg, runDesc.ReportLocation, STRunnerInputParamsArg, paramsFilePath); } else { argumentString = String.Format("{0} \"{1}\" {2} \"{3}\"", STRunnerTestArg, testinf.TestPath, STRunnerReportArg, runDesc.ReportLocation); } Stopwatch s = Stopwatch.StartNew(); runDesc.TestState = TestState.Running; if (!ExecuteProcess(fileName, argumentString, ref errorReason)) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = errorReason; } else { runDesc.ReportLocation = Path.Combine(runDesc.ReportLocation, "Report"); if (!File.Exists(Path.Combine(runDesc.ReportLocation, "Results.xml")) && !File.Exists(Path.Combine(runDesc.ReportLocation, "run_results.html"))) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = "No Results.xml or run_results.html file found"; } } //File.Delete(paramsFilePath); runDesc.Runtime = s.Elapsed; return(runDesc); }
/// <summary> /// runs the given test /// </summary> /// <param name="testinf"></param> /// <param name="errorReason"></param> /// <param name="runCancelled">cancellation delegate, holds the function that checks cancellation</param> /// <returns></returns> public TestRunResults RunTest(TestInfo testinf, ref string errorReason, RunCancelledDelegate runCancelled) { TestRunResults runDesc = new TestRunResults(); ConsoleWriter.ActiveTestRun = runDesc; ConsoleWriter.WriteLine(DateTime.Now.ToString(Launcher.DateFormat) + " Running: " + testinf.TestPath); runDesc.TestPath = testinf.TestPath; // check if the report path has been defined if (!string.IsNullOrWhiteSpace(testinf.ReportPath)) { runDesc.ReportLocation = testinf.ReportPath; ConsoleWriter.WriteLine(DateTime.Now.ToString(Launcher.DateFormat) + " Report path is set explicitly: " + runDesc.ReportLocation); } else if (!String.IsNullOrEmpty(testinf.ReportBaseDirectory)) { if (!Helper.TrySetTestReportPath(runDesc, testinf, ref errorReason)) { return(runDesc); } ConsoleWriter.WriteLine(DateTime.Now.ToString(Launcher.DateFormat) + " Report path is generated under base directory: " + runDesc.ReportLocation); } else { // default report location is the next available folder under test path // for example, "path\to\tests\APITest\Report123", the name "Report123" will also be used as the report name string reportBasePath = testinf.TestPath; string testReportPath = Path.Combine(reportBasePath, "Report" + DateTime.Now.ToString("ddMMyyyyHHmmssfff")); int index = 0; while (index < int.MaxValue) { index++; string dir = Path.Combine(reportBasePath, "Report" + index.ToString()); if (!Directory.Exists(dir)) { testReportPath = dir; break; } } runDesc.ReportLocation = testReportPath; ConsoleWriter.WriteLine(DateTime.Now.ToString(Launcher.DateFormat) + " Report path is automatically generated: " + runDesc.ReportLocation); } runDesc.ErrorDesc = errorReason; runDesc.TestState = TestState.Unknown; if (!Helper.IsServiceTestInstalled()) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = string.Format(Resources.LauncherStNotInstalled, System.Environment.MachineName); ConsoleWriter.WriteErrLine(runDesc.ErrorDesc); Environment.ExitCode = (int)Launcher.ExitCodeEnum.Failed; return(runDesc); } _runCancelled = runCancelled; if (!_stCanRun) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = Resources.STExecuterNotFound; return(runDesc); } string fileName = Path.Combine(_stExecuterPath, STRunnerName); if (!File.Exists(fileName)) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = Resources.STExecuterNotFound; ConsoleWriter.WriteErrLine(Resources.STExecuterNotFound); return(runDesc); } //write the input parameter xml file for the API test string paramFileName = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 10); string tempPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestParams"); Directory.CreateDirectory(tempPath); string paramsFilePath = Path.Combine(tempPath, "params" + paramFileName + ".xml"); string paramFileContent = testinf.GenerateAPITestXmlForTest(); string argumentString = ""; if (!string.IsNullOrWhiteSpace(paramFileContent)) { File.WriteAllText(paramsFilePath, paramFileContent); argumentString = String.Format("{0} \"{1}\" {2} \"{3}\" {4} \"{5}\"", STRunnerTestArg, testinf.TestPath, STRunnerReportArg, runDesc.ReportLocation, STRunnerInputParamsArg, paramsFilePath); } else { argumentString = String.Format("{0} \"{1}\" {2} \"{3}\"", STRunnerTestArg, testinf.TestPath, STRunnerReportArg, runDesc.ReportLocation); } Stopwatch s = Stopwatch.StartNew(); runDesc.TestState = TestState.Running; if (!ExecuteProcess(fileName, argumentString, ref errorReason)) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = errorReason; } else { // consider backward compatibility, here move the report folder one outside // that is, after test run, the report file might be at "path\to\tests\APITest1\Report123\Report\run_results.html" // here move the last directory "Report" one level outside, which is, "path\to\tests\APITest1\Report123\run_results.html" string apiTestReportPath = Path.Combine(runDesc.ReportLocation, "Report"); // apiTestReportPath: path\to\tests\APITest1\Report123\Report string targetReportDir = Path.GetDirectoryName(apiTestReportPath); // reportDir: path\to\tests\APITest1\Report123 string reportBaseDir = Path.GetDirectoryName(targetReportDir); // reportBaseDir: path\to\tests\APITest1 string tmpDir = Path.Combine(reportBaseDir, "tmp_" + DateTime.Now.ToString("ddMMyyyyHHmmssfff")); // tmpDir: path\to\tests\APITest1\tmp_ddMMyyyyHHmmssfff string tmpReportDir = Path.Combine(tmpDir, "Report"); // tmpReportDir: path\to\tests\APITest1\tmp_ddMMyyyyHHmmssfff\Report // since some files might not be closed yet, move the report folder might fail // so here will try a few times to move folder and let it as is (not moved) if still failed after several retry bool moveSuccess = false; string lastMoveError = string.Empty; int retry = 10; while (retry >= 0) { try { // steps: // 1. move directory "path\to\tests\APITest1\Report123" to "path\to\tests\APITest1\tmp_ddMMyyyyHHmmssfff" Directory.Move(targetReportDir, tmpDir); // 2. move directory "path\to\tests\APITest1\tmp_ddMMyyyyHHmmssfff\Report" to "path\to\tests\APITest1\Report123" Directory.Move(tmpReportDir, targetReportDir); // 3. delete empty directory "path\to\test1\tmp_ddMMyyyyHHmmssfff" Directory.Delete(tmpDir, true); // 4. update report location runDesc.ReportLocation = targetReportDir; moveSuccess = true; break; } catch (Exception ex) { lastMoveError = ex.Message; retry--; System.Threading.Thread.Sleep(500); } } if (!moveSuccess) { ConsoleWriter.WriteLine("Warning: Failed to change the report folder structure. " + lastMoveError); } if (!File.Exists(Path.Combine(runDesc.ReportLocation, "Results.xml")) && !File.Exists(Path.Combine(runDesc.ReportLocation, "run_results.html"))) { runDesc.TestState = TestState.Error; runDesc.ErrorDesc = "No Results.xml or run_results.html file found"; } } //File.Delete(paramsFilePath); runDesc.Runtime = s.Elapsed; return(runDesc); }