首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何让“RedirectStandardOutput”在NUnit工作?

我如何让“RedirectStandardOutput”在NUnit工作?
EN

Stack Overflow用户
提问于 2011-12-01 23:56:19
回答 2查看 1.1K关注 0票数 0

我正在为QA组制定自动化策略,需要能够捕获脚本和EXE文件的输出。当我将此代码作为控制台应用程序运行时,我能够成功地捕获plink.exe的输出:

代码语言:javascript
复制
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的值失败:

代码语言:javascript
复制
[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机器上执行)。

我希望我错过了一些简单的东西-我不想找到一个不同的测试线束。

它看起来与(未解决的)问题类似:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-02 17:06:25

好吧,花了我一晚上的时间,但我想明白了。除了重定向标准输出以使其工作外,我还必须使用RedirectStandardInput。

以下是在DLL文件中工作的固定代码。作为FYI,此修补程序还解决了Windows窗体应用程序中的问题:

代码语言:javascript
复制
[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");
    }
}
票数 1
EN

Stack Overflow用户

发布于 2013-08-30 16:20:06

就像你自己发现的那样,加上一行

代码语言:javascript
复制
process.StartInfo.RedirectStandardOutput = true;

解决了问题。NUnit必须设置另一个间接级别。谢谢你自己的回答让我免于痛苦的调查。

虽然我不认为这个问题来自于DLL文件/EXE文件之间的差异,因为我在一个作为控制台应用程序编译的测试项目中遇到了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8350074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档