问题
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