Add custom message to unit test result

2019-02-04 05:58发布

问题:

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.