Way to print message to debug output with Visual Studio CppUnitTestFramework - visual-studio

Is there a way to print messages to output window with CppUnitTestFramework in Visual Studio.
There is TRACE() function to display messages from program in the debugger Output window in MFC.
I want to know whether that kind of function exists or not with CppUnitTestFramework.

The Logger class in the CppUnitTestFramework namespace has two simple functions for this:
Logger::WriteMessage(const wchar_t* message);
Logger::WriteMessage(const char* message);
In the VS IDE, the output can be seen in the "Tests" console output window and can also by clicking the "Output" link seen in the detail panel at the bottom of the Test Explorer.
On a build server the message will appear in the log for the test phase.

Related

Use a console window when debugging unit-tests in Visual Studio

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 .

Visual Studio Debug Messages without noise

I want to write message while code is running (stopping changes the results), I know Debug.Print will write to Output or Immediate Windows in VS 2017 but so does all kinds of stuff I don't care about including Telemetry Messages and System.Windows.Data Error 2 and 4" from Visual Studio. I have disabled everything under Options/Debug/Output Window except "All Debug Output". Is it possible to sent all of Visual Studio stuff (listed under Options..) to the Output Window and only send message I write to Immediate Window or can I create my own Window with just me messages? The application runs for hours and only infrequently outputs a message but VS writes almost continuously so my messages are lost.
The "Debug Trace Logger" by Mads Kristensen does exactly what I was looking for, thanks for the link.

OutputDebugString doesn't print information in Visual Studio 2010

I've been trying to get OutputDebugString to work with no avail. My code follows:
int main(int argc, char* argv[])
{
OutputDebugStringA("asd");
getchar();
return 0;
}
When I run the application without debugging, DebugView shows asd but when I debug my application (F5), it doesn't print my string in Output or Intermediate window.
I've tried OutputDebugString with asd but nothing changed. What can be the problem?
You should terminate the string with "\n", but this should be only a cosmetic thing. I have tried your code right now and I can see the asd string in the Output window of VS 2010 without any issues.
Note: in the context menu of the Output pane (accessible using right mouse click) you can select which information should be displayed in the pane. Make sure you have enabled "Program output". Also make sure you are watching the correct window (Output or Immediate) depending on your setting "Redirect all Output window text to the Immediate window" in Tools/Options/Debugging/General.
We had a similar issue with x64 mixed mode debugging, so if you are using 64 bit check the Debugging property page for the main startup project. The Debugger Type should be set to 'Mixed' rather than 'Auto' to see native as well as managed debug output.
See here for more info.

Win32 Application Console Output?

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

Trace in Visual Studio Testing (Migrating from NUnit)

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

Resources