25

Can anyone tell me how to enable or get debug or console text output when running unit tests in a native (C++) test project in Visual Studio 2012?

Answer should not include the phrase, "use Google Test".

3
  • Will switching subsystem to console help? (project properties/linker/system) Commented May 29, 2015 at 11:31
  • Nice idea but alas, no. My code is outputting text using OutputDebugStringA and OutputDebugStringW. I guess I need to use some other library? Commented May 29, 2015 at 11:33
  • 4
    I kind of solved it. The debug output shows in the debug output window if you right-click and choose `debug' on whichever test in the test explorer window. Otherwise it doesn't. Commented May 29, 2015 at 11:41

3 Answers 3

24

In the unit test code, you can use

Logger::WriteMessage("My message string");

and the message will appear in the Output window.

Sign up to request clarification or add additional context in comments.

2 Comments

Works okay if you have #include "CppUnitTest.h" and are in the context of a TEST_METHOD in a TEST_CLASS but not just anywhere.
Works if you add: "using Microsoft::VisualStudio::CppUnitTestFramework::Logger" at the top
9

Brian's answer is correct. Use

Logger::WriteMessage("some text");

and run the test normally (there is no need to debug). However, you also need to select "Tests" in the "Show output from" drop-down list at the top of the Output window.

VS 2013 Output window with Tests output selected

1 Comment

The UI in Visual Studio 2019 remains the same: there's a "Tests" selection in the top of the "Output" window, and that's where the output will appear.
2

The debug output shows in the debug output window if you right-click and choose `debug' on whichever test in the test explorer window. Otherwise it doesn't.

2 Comments

Main Menu -> Test -> Windows -> Test Explorer, then right-click on one of the tests in the window to bring up its context menu and choose debug from there.
This seems to be true in VS 2017, using OutputDebugString(L"Message");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.