Outputting text in unit tests
When I run my unit tests, I would like to print out and read how long it takes to run a function. I tried using Console.WriteLine() and Trace.WriteLine(), but that didn't 开发者_如何学Cwork. What is the proper method I should be using?
I have the following unit test
[TestMethod()]
public void ProductSerializationTest()
{
Stopwatch swSerialization = new Stopwatch();
swSerialization.Start();
SerializeProductsToXML(dummyProductList, XMLFolderPath);
swSerialization.Stop();
// Print out swSerialization.Elapsed value
}
If you're using Visual Studio with its built-in testing support, the output of System.Diagnostics.Trace.WriteLine
will go to the test results report. That is, after your test has run, double-click its entry in the Test Results list, and there will be a section called "Debug Trace" in the test run report.
I had the same problem. I eventually found the solution.
Notice the green check or red x when you run the test in Visual Studio? Now click that and then click the link that says output.
That shows all the Trace.WriteLine()
s for me, even when not in debug mode.
Since you're using Microsoft.VisualStudio.TestTools.UnitTesting
, it would be most reasonable to call TestContext.WriteLine(). Here's how to use a TestContext.
Since I use TestDriven and NUnit, I just use Console.WriteLine()
and the messages show when I run tests with the debugger.
精彩评论