RedirectStandardOutput 在 NUnit 或 XUnit 中不起作用

问题描述 投票:0回答:0

我需要使用进程获取程序的版本。它在我的 WinForms 应用程序中工作,但当我将它放入测试中时,它总是返回 null。我在 NUnit 和 XUnit 中尝试过,但没有成功。

using(var p = new Process()) {
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.FileName = "executable.exe";
    p.StartInfo.Arguments = "--version";
    p.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
    p.ErrorDataReceived += (s, e) => Console.WriteLine(e.Data);
    p.Start();
    p.BeginOutputReadLine();
    p.WaitForExit();
}

我不想复活这个SO问题因为它已经有十年了。

c# unit-testing testing nunit xunit
© www.soinside.com 2019 - 2024. All rights reserved.