c# - 使用进程调用 cmd.exe 命令的 NUnit 测试未通过
问题描述
所以我正在创建一个 NUnit 项目,其中每个测试:
- 运行一个新
Process
的System.Diagnostics
- 用于
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,但这是项目的要求)。
我的想法用完了,需要帮助!:)
解决方案
属性下的方法[SetUp]
将在每次执行每个测试用例/方法之前执行。
确保在此方法中执行另一个测试用例之前终止所有先前的进程。
推荐阅读
- python - 实时心电图过滤
- visual-studio-code - 默认情况下在源代码管理窗口中打开文件而不是打开更改
- angular - Mat-datepicker - 如何在输入字段中显示用户保存的值 - [(ngModel)]
- bash - sed 在匹配模式后有条件地附加到行
- wordpress - 为空时隐藏 ACF 字段
- node.js - 节点重定向到一个数组的随机链接
- reactjs - 如何在页面加载时调度 redux 操作以在 useEffect 中加载数据
- react-native - 为 ios 和 android 动态更新应用构建版本 - React Native
- javascript - res.json() 创建 CastError
- java - 从 java 在 MacOs 终端中运行命令