我正在为QA组制定自动化策略,需要能够捕获脚本和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!");
}
}
}这个命令需要大约一分钟的时间执行,我成功地将结果捕获到输出变量。
但是,当我编译与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");
}
}我已经把问题缩小到了string output = process.StandardOutput.ReadToEnd()线上。如果我将行注释掉,则执行时间约为一分钟,该操作将在远程计算机上成功执行(test.sh在远程Linux机器上执行)。
我希望我错过了一些简单的东西-我不想找到一个不同的测试线束。
它看起来与(未解决的)问题类似:
发布于 2011-12-02 17:06:25
好吧,花了我一晚上的时间,但我想明白了。除了重定向标准输出以使其工作外,我还必须使用RedirectStandardInput。
以下是在DLL文件中工作的固定代码。作为FYI,此修补程序还解决了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");
}
}发布于 2013-08-30 16:20:06
就像你自己发现的那样,加上一行
process.StartInfo.RedirectStandardOutput = true;解决了问题。NUnit必须设置另一个间接级别。谢谢你自己的回答让我免于痛苦的调查。
虽然我不认为这个问题来自于DLL文件/EXE文件之间的差异,因为我在一个作为控制台应用程序编译的测试项目中遇到了这个问题。
https://stackoverflow.com/questions/8350074
复制相似问题