【发布时间】:2021-06-24 17:52:07
【问题描述】:
所以我正在创建一个 NUnit 项目,其中每个测试:
- 从
System.Diagnostics运行一个新的Process - 使用
cmd.exe调用lli.exe,并将LLVM 代码文件作为参数 - 检查此命令的退出代码和输出
一切顺利,测试在单独运行时通过,从测试资源管理器中逐一运行。但是,当我尝试在一次运行中运行多个测试时,就会出现问题。这是我的代码:
[Theory]
[TestCase("TestFile1", "")]
[TestCase("TestFile2", "")]
[TestCase("TestFile3", "0X17FFAA")]
public void TestValidProgram(string programPath, string expectedOutput = "")
{
Compiler.Compile(programPath); // here {programPath}.ll file is created
string result;
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/C {lliPath} {programPath}.ll",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process proc = new Process())
{
proc.StartInfo = startInfo;
proc.Start();
proc.WaitForExit();
result = proc.StandardOutput.ReadToEnd();
Console.WriteLine($"res: {result}");
Assert.AreEqual(0, proc.ExitCode); // stopping here
proc.Close();
}
Assert.AreEqual(expectedOutput, result);
}
当我在一次运行中运行所有 3 个测试时,只有第一个 TestFile1 通过,其余在此断言处停止:Assert.AreEqual(0, proc.ExitCode); 与 proc.ExitCode 等于 1。此外,result 字符串是空(因为它不应该是TestFile3)。
并行运行不是这种情况,我按顺序运行它们。此外,添加 [NonParallelizable] 属性不会改变任何内容。 Compiler创建的文件是正确创建的,可以使用lli.exe成功运行。
我使用 VSCode 2019 16.10.2。该项目在 .Net Framework 4.8 和 NUnit 3.13.2 上运行。 (我知道我可以使用 .net core 或 .net5,但这是项目的要求)。
我的想法用完了,需要帮助! :)
【问题讨论】: