发送和执行多个测试文件时出错
本文关键字:测试 文件 出错 执行 | 更新日期: 2023-09-27 18:20:28
我正在编写一个启动测试程序(.cmd)的应用程序,因此我正在通过已输入列表框的测试。如果有一个测试输入,这种方法可以很好地工作,但如果有2个或更多,它会给我错误:
"类型为"System.ComponentModel.Win32Exception"的未处理异常发生在System.dll中附加信息:系统找不到指定的文件"
StartInfo.Filename
和currentTestFromListbox[i]
在调试器中看起来都是正确的。
有人知道我哪里错了吗?
我很抱歉我的代码让人困惑——我只是个初学者。
public void executeCommandFiles()
{
int i = 0;
int ii = 0;
int numberOfTests = listboxTestsToRun.Items.Count;
executeNextTest:
var CurrentTestFromListbox = listboxTestsToRun.Items.Cast<String>().ToArray();
string filenameMinusCMD = "error reassigning path value";
int fileExtPos = CurrentTestFromListbox[i].LastIndexOf(".");
if (fileExtPos >= 0)
{
filenameMinusCMD = CurrentTestFromListbox[i].Substring(0, fileExtPos);
}
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = @"pushd Y:'Tests'" + filenameMinusCMD + @"'" + CurrentTestFromListbox[i];
startInfo.WorkingDirectory = @"pushd Y:'Tests'" + filenameMinusCMD + @"'";
startInfo.FileName = CurrentTestFromListbox[i];
Process.Start(startInfo);
//Wait for program to load before selecting main tab
System.Threading.Thread.Sleep(10000);
//Select MainMenu tab by sending a left arrow keypress
SendKeys.Send("{LEFT}");
i++;
if (i < numberOfTests)
{
checkIfTestIsCurrentlyRunning:
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains("nameOfProgramIAmTesting"))
{
System.Threading.Thread.Sleep(2000);
//if (ii > 150)
if (ii > 6) //test purposes only
{
MessageBox.Show("The current test (" + filenameMinusCMD + ") timed out at 5 minutes. The next test has been started.", "Test timed out",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
ii++;
goto checkIfTestIsCurrentlyRunning;
}
goto executeNextTest;
}
}
}
}
谢谢!-Joel
这是您重新考虑的代码。睡眠/gotos真的很困扰我。我无法真正测试它,但我认为它应该同样有效。如果它不起作用或者你有任何问题,请告诉我。
这假设你的列表框中有这样的内容:
testname.cmd
test2.cmd
test3.exe
lasttest.bat
这是我的尝试:
public void executeCommandFiles()
{
foreach (string test in listboxTestsToRun.Items)
{
//gets the directory name from given filename (filename without extension)
//assumes that only the last '.' is for the extension. test.1.cmd => test.1
string testName = test.Substring(0, test.LastIndexOf('.'));
//set up a FileInfo object so we can make sure the test exists gracefully.
FileInfo testFile = new FileInfo(@"Y:'Tests'" + testName + "''" + test);
//check if it is a valid path
if (testFile.Exists)
{
ProcessStartInfo startInfo = new ProcessStartInfo(testFile.FullName);
//get the Process object so we can wait for it to finish.
Process currentTest = Process.Start(startInfo);
//wait 5 minutes then timeout (5m * 60s * 1000ms)
bool completed = currentTest.WaitForExit(300000);
if (!completed)
MessageBox.Show("test timed out");
//use this if you want to wait for the test to complete (indefinitely)
//currentTest.WaitForExit();
}
else
{
MessageBox.Show("Error: " + testFile.FullName + " was not found.");
}
}
}