//鲁棒性测试 需要重新看一下 暂时没啥用 public TestLine ExecuteWrongTest(string arguments, string args) { TestLine testResult = new TestLine { testcase = args }; int timeLimit = 60; if (!FindExePath(_binaryInfo)) { testResult.passed = TestLine.FAIL; testResult.res = "No subway.exe file!"; return(testResult); } _binaryInfo.Arguments = arguments; _binaryInfo.RedirectStandardOutput = true; _binaryInfo.RedirectStandardError = true; try { using (Process exeProcess = Process.Start(_binaryInfo)) { StringBuilder sb = new StringBuilder(); exeProcess.WaitForExit(timeLimit * 1000); // 异步有时候读不到输出??? 暂时改成同步 sb.Append(exeProcess.StandardOutput.ReadToEnd()); //Release all resources if (!exeProcess.HasExited) { //Give system sometime to release resource exeProcess.Kill(); Thread.Sleep(1000); } if (sb.Length == 0) { testResult.passed = TestLine.FAIL; testResult.res = "No Information"; return(testResult); } else { testResult.passed = TestLine.SUCCESS; testResult.res = sb.ToString(); return(testResult); } } } catch (Exception e) { testResult.passed = TestLine.SUCCESS; testResult.res = "The process terminated. Wrong!"; return(testResult); } }
//检测路径的正确性 public TestLine ExecuteRoutineTest(string arguments, string number, int timeLimit) { TestLine testResult = new TestLine { testcase = number }; if (!FindExePath(_binaryInfo)) { Logger.Error("No subway.exe file!", _logFile); testResult.passed = TestLine.FAIL; testResult.res = ErrorType.NoSubwayExe.ToString(); return(testResult); } int num = int.Parse(Regex.Match(number, @"\d+").Value); _binaryInfo.Arguments = arguments; try { Stopwatch timeWatch = new Stopwatch(); timeWatch.Start(); // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = Process.Start(_binaryInfo)) { //Start monitor exeProcess.WaitForExit(timeLimit * 1000); timeWatch.Stop(); //Release all resources if (!exeProcess.HasExited) { //Give system sometime to release resource exeProcess.Kill(); Thread.Sleep(1000); } } //Check the station file string checkFile = Path.Combine(_binaryInfo.WorkingDirectory, "routine.txt"); if (!File.Exists(checkFile)) { Logger.Info("No routine.txt file!", _logFile); testResult.passed = TestLine.FAIL; testResult.res = ErrorType.NoGeneratedStationTxt.ToString(); return(testResult); } //如果不出现错误的话,则退出 int tryTimeLimit = 10; while (tryTimeLimit > 0) { try { File.ReadAllText(checkFile); break; } catch (Exception) { Thread.Sleep(100); tryTimeLimit--; } } if (tryTimeLimit == 0) { Logger.Info("Exe run time out!", _logFile); testResult.passed = TestLine.FAIL; testResult.res = ErrorType.OutOfTimeCloseExe.ToString(); return(testResult); } //获取错误代码 //获取arguments的-b后面的2个参数 ErrorType errorCode = CheckRoutineValid(checkFile, num); if ((int)errorCode > 0) { Logger.Info( $"Arguments:{arguments} Normal, spend time {(double)timeWatch.ElapsedMilliseconds / 1000}s", _logFile); testResult.passed = TestLine.SUCCESS; testResult.res = Convert.ToString((double)timeWatch.ElapsedMilliseconds / 1000); return(testResult); } testResult.passed = TestLine.FAIL; testResult.res = errorCode.ToString(); return(testResult); } catch (Exception e) { //Log into file to record the runtime error Logger.Error($"Arguments:{arguments} RuntimeError:{e.Message}", _logFile); testResult.passed = TestLine.FAIL; testResult.res = ErrorType.RuntimeError.ToString(); return(testResult); } }