[英]How do I get 'RedirectStandardOutput' to work in NUnit?
I am working on an automation strategy for our QA group and need to be able to capture the output of scripts and EXE files.我正在为我们的 QA 小组制定自动化策略,需要能够捕获脚本和 EXE 文件的输出。 When I run this code as a console application, I am able to successfully capture the output of plink.exe:当我将此代码作为控制台应用程序运行时,我能够成功捕获 plink.exe 的输出:
class Program
{
static void Main(string[] args)
{
Process process = new Process();
process.StartInfo.FileName = @"C:\Tools\plink.exe";
process.StartInfo.Arguments = @"10.10.9.27 -l root -pw PASSWORD -m C:\test.sh";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
output = output.Trim().ToLower(); // Output is successfully captured here
if (output == "pass")
{
Console.WriteLine("Passed!");
}
}
}
This command takes about a minute to execute and I successfully capture the results to the output variable.执行此命令大约需要一分钟,我成功地将结果捕获到输出变量中。
However, when I compile the same code as a DLL and run through NUnit, the code completes immediately and fails with the value of output == NULL:但是,当我编译与 DLL 相同的代码并通过 NUnit 运行时,代码立即完成并失败,输出值 == NULL:
[TestFixture]
public class InstallTest
{
[Test]
public void InstallAgentNix()
{
Process process = new Process();
process.StartInfo.FileName = @"C:\Tools\plink.exe";
process.StartInfo.Arguments = @"10.10.9.27 -l root -pw PASSWORD -m C:\test.sh";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
output = output.Trim().ToLower();
Assert.AreEqual("pass", output, "Agent did not get installed");
}
}
I have narrowed the problem down to the line string output = process.StandardOutput.ReadToEnd()
.我已将问题缩小到行string output = process.StandardOutput.ReadToEnd()
。 If I comment the line out, execution time is around a minute and the operation is successfully executed on the remote machine (test.sh gets executed on a remote Linux box).如果我注释掉该行,执行时间大约为一分钟,并且操作在远程机器上成功执行(test.sh 在远程 Linux 机器上执行)。
I hope I'm missing something simple - I don't want to have to find a different test harness.我希望我错过了一些简单的东西 - 我不想找到不同的测试工具。
It looks similar to the (unsolved) question here: Why does a process started in a DLL file work when tested using console application, but not when called by another DLL file?它看起来类似于此处的(未解决的)问题: 为什么在使用控制台应用程序测试时在 DLL 文件中启动的进程可以工作,但在被另一个 DLL 文件调用时却不能工作?
OK, it took me all night but I figured it out.好吧,我花了一整夜,但我想通了。 I have to RedirectStandardInput in addition to redirecting standard output for this to work.除了重定向标准输出以使其正常工作之外,我还必须重定向标准输入。
Here is the fixed code that works in a DLL file.这是在 DLL 文件中工作的固定代码。 As an FYI, this fix resolves the problem in a Windows Forms application as well:作为仅供参考,此修复程序也解决了 Windows 窗体应用程序中的问题:
[TestFixture]
public class InstallTest
{
[Test]
public void InstallAgentNix()
{
Process process = new Process();
process.StartInfo.FileName = @"C:\Tools\plink.exe";
process.StartInfo.Arguments = @"10.10.9.27 -l root -pw PASSWORD -m C:\test.sh";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
output = output.Trim().ToLower();
Assert.AreEqual("pass", output, "Agent did not get installed");
}
}
As you found by yourself, adding the line正如您自己发现的那样,添加行
process.StartInfo.RedirectStandardOutput = true;
solves the problem.解决了这个问题。 NUnit must set up another level of indirection. NUnit 必须设置另一个间接级别。 Thanks for your own answer which saved me from painful investigation.感谢您的回答,使我免于痛苦的调查。
Although I don't think the issue comes from the difference between DLL file/EXE file since I encountered this in a test project compiled as a console application.虽然我不认为问题来自 DLL 文件/EXE 文件之间的差异,因为我在编译为控制台应用程序的测试项目中遇到了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.