从代码运行 NUnit - 如何将结果输出到控制台? [英] Running NUnit from code - how can I output results to the console?

查看:51
本文介绍了从代码运行 NUnit - 如何将结果输出到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RemoteTestRunner 运行 NUnit 测试.最后,我得到一个包含结果的 TestResult 对象.单元测试项目编译为控制台应用程序.问题是,在运行测试后,输出以某种方式重定向,我无法将结果打印到控制台.

这是代码.它不输出任何内容,甚至不输出打开,芝麻!"(尽管它确实运行到最后 - 在调试器中确认).

有什么建议吗?

另外,给定 TestResults 实例,是否有内置的方法来列出失败的结果?

public static void Main(){TestPackage testPackage = new TestPackage(AssemblyPath);RemoteTestRunner remoteTestRunner = new RemoteTestRunner();remoteTestRunner.Load(testPackage);TestResult testResult = remoteTestRunner.Run(null);Console.WriteLine(testResult.IsFailure);Console.WriteLine(打开,芝麻!");}公共静态字符串 AssemblyPath{得到{string codeBase = Assembly.GetExecutingAssembly().CodeBase;UriBuilder uri = new UriBuilder(codeBase);字符串路径 = Uri.UnescapeDataString(uri.Path);返回路径;}}

解决方案

在运行测试之前尝试存储控制台的当前输出流

var currentOut = Console.Out;

然后在运行完成后将其设置回

Console.SetOut(currentOut);

<块引用>

此外,是否有一种内置方法可以列出给定 TestResults 实例的失败结果?

我一直找不到.但是,以下代码应该可以为您提供一些帮助.它递归地内省组合的 TestResult 结构并将每个测试的结果输出到控制台.

static void OutputResult(TestResult result){如果(结果.有结果){foreach (var childResult in result.Results){输出结果((TestResult)childResult);}返回;}Console.WriteLine("{0}:{1}", result.FullName, result.ResultState);}

I am running NUnit tests using RemoteTestRunner. In the end, I get a TestResult object containing the results. The unit test project compiles as a console application. The problem is, after running the tests, the output gets somehow redirected, and I can't print the results to the console.

Here's the code. It doesn't output anything, not even "Open, sesame!" (although it does run to the end - confirmed in the debugger).

Any suggestions?

Also, is there a built-in way to list the failed results, given the TestResults instance?

public static void Main()
{
    TestPackage testPackage = new TestPackage(AssemblyPath);
    RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
    remoteTestRunner.Load(testPackage);
    TestResult testResult = remoteTestRunner.Run(null);

    Console.WriteLine(testResult.IsFailure);

    Console.WriteLine("Open, sesame!");

}
public static string AssemblyPath
{
    get
    {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        return path;
    }
}

解决方案

Try to store the current output stream of the Console before running the tests with

var currentOut = Console.Out;

Then setting it back once the run has been performed with

Console.SetOut(currentOut);

Also, is there a built-in way to list the failed results given the TestResults instance?

I haven't been able to find any. However, the following piece of code should provide you with some help. It recursively introspects the composed TestResult structure and outputs the result of each test to the console.

static void OutputResult(TestResult result)
{
    if(result.HasResults)
    {
        foreach (var childResult in result.Results)
        {
            OutputResult((TestResult)childResult);
        }
        return;
    }

    Console.WriteLine("{0}:{1}", result.FullName, result.ResultState);
}

这篇关于从代码运行 NUnit - 如何将结果输出到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆