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!
Debug. WriteLine will display in the output window ( Ctrl + Alt + O ), you can also add a TraceListener to the Debug. Listeners collection to specify Debug.
In Visual Studio uppermost menu choose Debug > Windows > Output. It shows all Console.
Use Debug. WriteLine to write debug output. The output will be displayed in your IDE. Assuming Visual Studio, it will write to the Output window when you run the project.
WriteLine(String) Writes a message followed by a line terminator to the trace listeners in the Listeners collection. WriteLine(Object) Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With