将自定义消息添加到单元测试结果

发布于 2024-10-09 21:26:26 字数 93 浏览 0 评论 0原文

有没有办法可以将自定义消息添加到测试方法的结果中?我想在部分代码上放一个秒表来看看它运行了多长时间。我不需要测试它是否在特定时间范围内运行,只想在结果窗口中查看经过的时间。

Is there a way I can add a custom message to the result of a test method? I want to put a stopwatch on part of the code to see how long its running. I don't need to test that it's running within a certain time frame, just want to see in the result window what the elapsed time was.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

雪花飘飘的天空 2024-10-16 21:26:27

根据消息的长度,您可能需要使用Console.WriteLine。它被添加到测试结果中,列名称Output (StdOut),因此您无需进入测试运行详细信息即可看到它。

我发现输出一些非常一般的信息很有用,例如测试的属性数量,或测试实际运行的任何其他证据。

如果您没有状态消息,如果有人破坏了您的代码并删除了测试方法主体中的所有内容怎么办?现在都过去了,但是一点帮助都没有。

Depending on the length of the message, you may want to use Console.WriteLine. It gets added to the test results, column name Output (StdOut), so you can see it without going into test run details.

I find it useful to output some very general information, such as the number of properties that were tested, or any other proof that the tests were actually run.

If you don't have a status message, what if somebody sabotaged your code and deleted everything from the test method body? They are all passing now, but it does not help at all.

烟若柳尘 2024-10-16 21:26:26

假设您使用的是 MSTest,您可以使用

System.Diagnostics.Trace.WriteLine("Hello World");

来输出您想要的任何内容。它将显示在测试运行程序的完整结果中。

Assuming you are using MSTest, you can use

System.Diagnostics.Trace.WriteLine("Hello World");

To output whatever you'd like. It will show up in the full results of the test runner.

青柠芒果 2024-10-16 21:26:26

上面的示例对我不起作用,但以下示例起作用:

var result = routeManager.UpdateWalkingOrder(previouspremiseFuncLoc, premiseFuncLoc, out message);

Assert.AreEqual(true, result, message);

其中消息是我想在测试结果的错误消息对话框中显示的任何错误。
在上面的示例中,我返回执行该函数时捕获的错误,并将其显示在我的测试结果中,这对调试有很大帮助。

在你的情况下,你可以只显示运行时间。

The example above did not work for me but the following did:

var result = routeManager.UpdateWalkingOrder(previouspremiseFuncLoc, premiseFuncLoc, out message);

Assert.AreEqual(true, result, message);

Where message is any error i want to display in the Error Message Dialog of my Test Result.
In the example above I am returning errors that are captured when executing that function and showing it in my test results which is great help for debugging.

In your case you could just display the running time.

仄言 2024-10-16 21:26:26

我走了同样的路线,试图找到一种向测试结果输出消息的方法。您只需将 elapsedtime 存储到 String 中,然后使用:

Console.WriteLine(elapsedtime);

对我来说效果很好。

I went the same route, trying to find a way to output a message to Test Results. You can simply store your elapsedtime into a String and just use:

Console.WriteLine(elapsedtime);

Worked for me just fine.

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