首页 > 解决方案 > 使用进程调用 cmd.exe 命令的 NUnit 测试未通过

问题描述

所以我正在创建一个 NUnit 项目,其中每个测试:

一切顺利,测试在单独运行时通过,从测试资源管理器中逐一运行。但是,当我尝试在一次运行中运行多个测试时,就会出现问题。这是我的代码:

        [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.ExitCode1。此外,result字符串是空的(因为它不应该是 的情况下TestFile3)。

并行运行不是这种情况,我按顺序运行它们。此外,添加[NonParallelizable]属性不会改变任何内容。创建的文件Compiler是正确创建的,并且可以使用lli.exe.

我使用 VSCode 2019 16.10.2。该项目在 .Net Framework 4.8 和 NUnit 3.13.2 上运行。(我知道我可以使用 .net core 或 .net5,但这是项目的要求)。
我的想法用完了,需要帮助!:)

标签: c#nunitlli

解决方案


属性下的方法[SetUp]将在每次执行每个测试用例/方法之前执行。

确保在此方法中执行另一个测试用例之前终止所有先前的进程。


推荐阅读