Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 738
Tim Hoolihan Avatar asked Aug 05 '09 16:08

Tim Hoolihan


2 Answers

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.

like image 152
Jason Evans Avatar answered Sep 28 '22 03:09

Jason Evans


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

System.Diagnostics.Debug.WriteLine("Message");
like image 32
Michael Alves Avatar answered Sep 28 '22 02:09

Michael Alves