问题
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
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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...?
回答6:
"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).
回答7:
CTRL + ALT + I shows you the immediate window
来源:https://stackoverflow.com/questions/173641/how-do-i-see-debug-writeline-statements-when-using-testdriven-net