在单元测试中输出文本

发布于 2024-09-19 22:41:13 字数 417 浏览 2 评论 0原文

当我运行单元测试时,我想打印并读取运行一个函数需要多长时间。我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

錯遇了你 2024-09-26 22:41:13

如果您使用带有内置测试支持的 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.

红墙和绿瓦 2024-09-26 22:41:13

我也有同样的问题。我最终找到了解决方案。

在 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.

帥小哥 2024-09-26 22:41:13

由于您使用的是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文