Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output log in MS Unit Testing Framework VS 2010

I am trying to log some information while the unit test is running in MS Unit Testing Framework VS 2010.

I tried Trace.WriteLine, Console.WriteLine and Debug.WriteLine but I am not able to see the output in the output window.

Any idea how to do it? Thanks in advance

like image 829
ganeshran Avatar asked Oct 25 '10 11:10

ganeshran


People also ask

Should you unit test log messages?

Unit tests are for testing business logic. If it's not business logic, don't test it in a unit test. If you have utility methods that assist logging, you should unit test those. If you want to "test" logging, do it as part of integration testing and assert that some stream (usually console) contains certain output.

How do you record a unit test?

To record unit test results to the pipeline's run log: Run unit tests in a pipeline step that produce a results file in junit format. Save the test results file using the save_tests utility function.

How do I run MSTest in Visual Studio?

Test shortcuts Tests can be run from Test Explorer by right-clicking in the code editor on a test and selecting Run test or by using the default Test Explorer shortcuts in Visual Studio.

What is difference between NUnit and MSTest?

MsTest is a native unit testing library that comes with Visual Studio from Microsoft. NUnit is an extra Nuget package that needs to be installed on top and interact with the APIs of Visual Studio. Nunit likely doesn't have direct integration into the native APIs like MsTest does.


2 Answers

Make sure your test class contains the following:

private TestContext testContextInstance;

/// <summary>
/// Gets or sets the test context which provides
/// information about and functionality for the current test run.
/// </summary>
public TestContext TestContext
{
    get
    {
        return testContextInstance;
    }
    set
    {
        testContextInstance = value;
    }
}

Then you can call:

this.testContextInstance.WriteLine("Hello World");
like image 52
Tom Robinson Avatar answered Sep 23 '22 18:09

Tom Robinson


The output from the test case is not visible in visual studio's output window. Rather it is visible in the "test results window". In the test result window, you should double click on the result of the test case (Passed/addTest row in the picture) for which you want to see the output and there you will see the all your writeLines.

alt text

like image 28
Aseem Bansal Avatar answered Sep 22 '22 18:09

Aseem Bansal