Runnin NUnit from code - how to output results to console?

大城市里の小女人 提交于 2019-12-12 02:48:29

问题


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 app. 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;
        }
    }

回答1:


Have you tried storing 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 intropects the composed TestResult structure and outputs to the Console the result of each test.

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);
}


来源:https://stackoverflow.com/questions/8819803/runnin-nunit-from-code-how-to-output-results-to-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!