Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting text in unit tests

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
 }
like image 741
burnt1ce Avatar asked Sep 17 '10 18:09

burnt1ce


People also ask

What is Testcontext Mstest?

Used to store information that is provided to unit tests.

Can tester write unit test?

Yes, developers typically write unit tests. However, they are largely responsible for writing these tests to ensure that the code works – most developer tests are likely to cover happy-path and obvious negative cases.


2 Answers

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.

like image 102
Eric Smith Avatar answered Oct 20 '22 23:10

Eric Smith


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.

like image 31
dreamwork801 Avatar answered Oct 21 '22 00:10

dreamwork801