views:

2002

answers:

6

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

+2  A: 

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.

Jon Limjap
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! http://www.testdriven.net/quickstart.aspx#Run%20Test(s)
Andrew
Just tried the Console.WriteLine() and it shows up in the Test window. I'll give that a go for now, thanks again :o)
Andrew
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)
Andrew
+1  A: 

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.

Andrew
Yep, that's why it is encouraged to have a single assert per test.
Jon Limjap
A: 

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...?

Marc Gravell
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/
Andrew
Interesting - cheers
Marc Gravell
A: 

"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).

Christian.K
+1  A: 

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.

Marc Gravell
Great! thanks for the update :o)
Andrew
+2  A: 

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.

jcansdale
+1 as you wrote it!
RichardOD