How do I see Debug.WriteLine statements when using TestDriven.Net?
Asked Answered
S

7

5

I'm trying to use TestDriven.Net not only to test my code, but to call a function on my code whose purpose is to print out the internal state of the code to the Debug window.

Here's a very simplified example of what I'm trying to do..

<TestFixture()> _
Public Class UnitTest

    <Test()> _
    Public Sub TestDebug()
        Dim oClass1 As New Class1

        Assert.AreEqual(True, oClass1.IsTrue)

        Debug.WriteLine("About to call .PrintDebug()")
        oClass1.PrintToDebug()

    End Sub

End Class

Public Class Class1

    Private _IsTrue As Boolean = True

    Public ReadOnly Property IsTrue() As Boolean
        Get
            Return _IsTrue
        End Get
    End Property

    Public Sub PrintToDebug()
        Debug.WriteLine("Internal state of Class1: " & _IsTrue)
    End Sub

End Class

I'm trying to test the Public interface of Class1, and somehow view the output from the Class1.PrintToDebug() function.

I've looked through the TestDriven.Net quickstart, which shows examples of using the Debug.WriteLine in a unit test, but strangely this doesn't work for me either - i.e. the only Output in my 'Test' window is:

------ Test started: Assembly: ClassLibrary1.dll ------


1 passed, 0 failed, 0 skipped, took 1.19 seconds.

I've tried looking in the other windows (Debug and Build), the Debug window has the 'Program Output' and 'Exception Messages' options enabled.

I've looked for options or preferences and can't find any!

Thanks for your help!


Edit: I'm using VB.Net 2.0, TestDriven.Net 2.14.2190 and NUnit 2.4.8.0
Severson answered 6/10, 2008 at 9:13 Comment(0)
B
11

I found that while Debug.Writeline() doesn't work with unit tests, Console.WriteLine() does.

The reason is that when you run tests, the debugger process isn't invoked, and Debug.WriteLine() is ignored. However, if you use "Test with Debugger", I think (haven't tried) Debug.WriteLine() will work.

Bois answered 6/10, 2008 at 9:17 Comment(3)
Thanks for the suggestion, but I don't get any output using the 'Test with Debugger' option either. Strange that their documentation shows 'Debug.WriteLine' and 'Run Tests' being clicked! testdriven.net/quickstart.aspx#Run%20Test(s)Severson
Just tried the Console.WriteLine() and it shows up in the Test window. I'll give that a go for now, thanks again :o)Severson
I'll accept your answer as changing the Debug.WriteLine() statements did the trick. Console.WriteLine worked, although I decided to use Trace.WriteLine instead :o)Severson
S
3

Trace.WriteLine() appears to be the answer :o)

Here's the output for the example from my question, using Trace instead of Debug:

------ Test started: Assembly: ClassLibrary1.dll ------

Internal state of Class1: True

1 passed, 0 failed, 0 skipped, took 0.61 seconds.

One thing I've found though.. execution is halted at the first failing unit test assertion, meaning that Trace statements aren't executed if an Assert() above them fails.

Severson answered 6/10, 2008 at 9:36 Comment(1)
Yep, that's why it is encouraged to have a single assert per test.Bois
A
2

Try using Trace.WriteLine(...) instead. The call to Debug.WriteLine(...) will only be made when DEBUG is defined. By default new Visual Studio projects no longer define DEBUG, but they do define TRACE.

I should really change the quickstart example to use Trace instead.

Regards, Jamie.

Agateware answered 3/12, 2009 at 12:57 Comment(2)
-1 does not work with trace either (but is configurable through the options)Frankenstein
and it prints out just after the test finishes!Frankenstein
E
1

You might want to know that 2.16 (the current beta version) includes:

1587: Always display console output/error and test runner messages

Test runner generated messages and console output will now be displayed when running all tests in a project/solution.

1588: Optionally display trace/debug output when running all tests in project/solution

By default trace/debug output isn't displayed when executing all tests in a project/solution. This behaviour can be modified via the TesDriven.Net options pane.

So it seems that it will work in the next version.

Emmen answered 10/10, 2008 at 10:30 Comment(0)
E
0

IIRC, this output is only shown in the output window when running an individual test. Try right-clicking in the test method to run just that test...?

Emmen answered 6/10, 2008 at 9:45 Comment(1)
That's how I'm doing it but still no output from Debug.WriteLine() - I've switched to Trace.WriteLine and it's working :o/Severson
K
0

"Run Tests..." picks up whatever setting you currently have to build your solution/project.

You have to make sure that the current build settings for your solution/project are set to "Debug" and not to "Release" (otherwise Debug.Write*() calls are conditionally removed by the compiler).

Kingofarms answered 6/10, 2008 at 11:34 Comment(0)
W
0

CTRL + ALT + I shows you the immediate window

Wandawander answered 29/4, 2013 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.