Tim Hoolihan
Tim Hoolihan

Reputation: 12396

Trace in Visual Studio Testing (Migrating from NUnit)

In NUnit, I'm used to writing Trace statements in the test, and having them show up in the trace tab of the NUnit gui.

On a new project, I'm moving to the built in Unit Testing in Visual Studio Professional Addition, which I believe is an interface to mstest.exe.

Test Code:

<TestMethod()>
Public Sub TestPagesInheritFromBasePage()
    Dim webUI As Assembly = Assembly.GetAssembly(GetType(WebUI.BasePage))
    Dim badPages As New List(Of String)
    For Each t As Type In webUI.GetTypes()
        Debug.Write(t.Name + ", ")
        Trace.Write(t.Name + ", ")
        If t.BaseType Is GetType(System.Web.UI.Page) Then badPages.Add(t.Name)
    Next
    Debug.Flush()
    Trace.Flush()
    If badPages.Count > 0 Then
        Assert.Fail("{0}: do not inheriting from BasePage", String.Join(", ", badPages.ToArray()))
    End If
End Sub

I'm getting a failure, so I know the Debug.Write and Trace.Write lines are executing.

I've read through the MSDN docs on writing these tests, and I can view the trace output if executing at the command line, via:

mstest.exe /testcontainer:mydll.dll /detail:debugtrace

However, I can not find the trace output when executing the tests directly in visual studio. Is there another preferred method of outputting information during a unit test, or am I missing an option to see trace info in visual studio?

Answer: Both of the answers below (Console.Write and Debug.Write) worked, the results were in Test Results Detail (TestResult Pane at the bottom, right click on on Test Results and go to TestResultDetails). Also, I set Debug and Trace constants in project properties.

Upvotes: 10

Views: 8419

Answers (4)

rmoestl
rmoestl

Reputation: 3155

All earlier answers are actually correct but require more or less mouse-clicking.

If you would like to see the output immediately without an extra click, just add the columns Debug Trace and/or Output (StdOut) (whether you're using Debug.Write or Console.Write) to the Test Results pane through right-clicking on the test result and then 'Add/Remove Columns'.

Upvotes: 1

Jason Evans
Jason Evans

Reputation: 29186

Try using Console.WriteLine() instead. I use that in my unit tests and it works fine - it displays text in unit test result output window.

Upvotes: 10

bnieland
bnieland

Reputation: 6496

To see the results double click on the test in the "Test Results" window (Accessed from the main menu item "Tests" >> window menu >> Test Results)

Upvotes: 2

Michael Alves
Michael Alves

Reputation: 635

Usually I use this method to print something in the output window of visual studio:

System.Diagnostics.Debug.WriteLine("Message");

Upvotes: 9

Related Questions