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.
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.
Usually I use this method to print something in the output window of visual studio:
System.Diagnostics.Debug.WriteLine("Message");
To see the results double click on the test in the "Test Results" window (Accessed from the main menu item "Tests" >> window menu >> Test Results)
All earlier answers are actually correct but require more or less mouse-clicking.
If you would like to see the output immediately without an extra click, just add the columns Debug Trace and/or Output (StdOut) (whether you're using Debug.Write or Console.Write) to the Test Results pane through right-clicking on the test result and then 'Add/Remove Columns'.
Related
here is my program
program hello
print *, "Hello World!"
end program hello
and it says to me
visual studio cannot debug because a debug target has not been specified
The way this normally happens is that you chose the incorrect project type - for example, a library instead of executable. If your project is properly building an EXE and you still get this error, right click on the project, select Properties. Then on the Debugging property page set the Command property to $(TargetPath). Otherwise, create a new project of the correct type (for example, Console Application
When I debug a unit-test with Visual Studio 2017, all the console output is logged and I can examine it by clicking the "Output"-link in the result box of the unit-test (I'm using the built-in Test-Explorer).
However, since I do log a lot to console at runtime which is even color coded so important stuff is more visible, I can't see this color coded output using the "Output"-link, because it's just plain black text on white background.
Is it possible that Visual Studio shows all the output directly in a console window when debugging the tests so I see the output as I would see it when actually running the application outside of a test?
You cannot (or it is very tricky at least) open a console window from a unit test - I've tried with the top 2 answers from the following post, and they didn't work: Show Console in Windows Application?
You can make the Debug.Write... methods write to the Console when you are running/debugging the application itself (not the unit tests) with this code, though:
ConsoleTraceListener listener = new ConsoleTraceListener();
Debug.Listeners.Add(listener);
Any calls to Debug.Write... methods after this code will also output to the console.
Standard output is the only way to display the test output, when we use some output method in our test like Debug.WriteLine(); Console.WriteLine(); etc.
I also tried to change the font of the test output but it seems there is no that option is VS 2017, even someone though someone mentioned this before:
Is there a way to change the Font used in the VS2012 Test Output Window?
Similar case discussed in this thread, even some reply claimed that we could manually launch the console window, but they are 100% sure about it.
If you really need change the font or the color of the test output, in Visual Studio you go to Help -> Send feedback to VS developing team to provider your suggestion .
I'm using XUnit, and in both the Visual Studio Test Explorer window and the Resharper Unit Test runner window, I'm no longer seeing the "output" hyperlink that would show the contents of WriteLine commands.
Is there a setting in the IDE that I need to change?
This is a change in xunit2. It no longer captures output from Console.WriteLine, etc. This is because it now runs tests in parallel, in multiple threads, and there's no way of knowing what test the output comes from. It is still possible to capture output, but you need to use xunit's ITestOutputHelper in order to do so. See the xunit docs for more details.
I am testing an F# project using NUnit. On the debug tab of project configuration I have set the debugger to use an external program which is nunit-console here and the working directory to the debug folder of my project. I prefer using nunit-console with the debugger since the GUI version doesn't hit the test file's breakpoints.
When I debug the test the console window appears and disappears and there is no chance to read the output. I have tried Console.Readline(), but it doesn't work because when I directly run the test from a terminal using nunit-console, it fails due to this command.
Is there a way to redirect the nunit-console output to the Visuals Studio's output window?
UPDATE: I could use Thread.Sleep() to delay the nunit-console.exe when I run the test from the console. But from Visual Studio it doesn't happen so I am pretty sure that nunit-console.exe fails to read the test file when the command is issued by Visual Studio. Still, it would be very nice to be able to read the console output, thus the redirection is still desirable.
Either use Tools->Options...->Debugging->General:"Redirect all Output Window text to Immediate Window" and then make sure that the "Immediate Window" is visible with Debug->Windows->Immediate.
Or use NUnit's "/wait" command line option.
Unless I am missing something, you should be able to hit all your breakpoints with the GUI as well, set the startup project to nunit.exe and pass the name of your test DLL as a command line parameter. You will hit the breakpoints in Visual Studio, and your print statments will be in the GUI's output tab.
When developing a Win32 Application (non-console application) in Visual Studio 2005, is there any way to get the same sort of output as you do from the console?
For instance, say I want to see log statements (such as I would with cout in a console application) to trace the path my program has taken in the code.
My first thought would be that this could be done through the Output tab selecting something from its "Show output from:" dropdown, when debugging but I don't know what API I need to do this...
For example say I had the following in my windows application and wanted to know when the following function enters and exits writing the result to the Visual Studio window above.
void someFunction(void)
{
Win32APIConsoleLog("BEGIN: someFunction()");
// ...
Win32APIConsoleLog("END: someFunction()");
}
Is this possible? And if so, what what libraries do I need to include and what function calls do I need to make to write out to the console window?
OutputDebugString.
I assume that you want to write to the debug console, since that's what your screenshot shows. OutputDebugString is a nop when no debugger is attached, but it allows you to log whatever you want to the debugger's output.
OutputDebugStringW(L"This will go to the output.\n");
// or
OutputDebugString("This will go to the output.\n");