在单元测试中输出文本
当我运行单元测试时,我想打印并读取运行一个函数需要多长时间。我尝试使用 Console.WriteLine() 和 Trace.WriteLine(),但这不起作用。我应该使用什么正确的方法?
我有以下单元测试
[TestMethod()]
public void ProductSerializationTest()
{
Stopwatch swSerialization = new Stopwatch();
swSerialization.Start();
SerializeProductsToXML(dummyProductList, XMLFolderPath);
swSerialization.Stop();
// Print out swSerialization.Elapsed value
}
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 work. 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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用带有内置测试支持的 Visual Studio,则
System.Diagnostics.Trace.WriteLine
的输出将转到测试结果报告。也就是说,您的测试运行后,双击“测试结果”列表中的条目,测试运行报告中将出现一个名为“调试跟踪”的部分。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.我也有同样的问题。我最终找到了解决方案。
在 Visual Studio 中运行测试时,注意到绿色勾号或红色 x 了吗?现在单击该按钮,然后单击显示输出的链接。
这为我显示了所有
Trace.WriteLine()
,即使不在调试模式下也是如此。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.由于您使用的是
Microsoft.VisualStudio.TestTools.UnitTesting
,因此最合理的做法是调用 TestContext.WriteLine()。以下是如何使用 TestContext。由于我使用 TestDriven 和 NUnit,因此我只使用
Console.WriteLine()
,并且当我使用调试器运行测试时会显示消息。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.