Add custom message to unit test result

时光怂恿深爱的人放手 提交于 2019-12-08 23:13:20

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/4589912/add-custom-message-to-unit-test-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!