How to get console output in Visual Studio 2012 Unit Tests
Asked Answered
S

4

24

I have a managed C++ unit test in VS 2012. The test runs fine and I can verify that a loop with multiple cout calls is executed.

However when I look at the test explorer the test is marked as passed but there is no hyper link for the output as I am used to for c# projects.

The code at the end of my test is

for (int i = 0; i < 4; i++)
{
    cout << parameters[i];
    cout << endl;
}

which I can verify runs as I step through it in the debugger. I have also tried with cerr but no difference.

Seeseebeck answered 29/5, 2013 at 13:53 Comment(2)
If I were to use Console.WriteLine or Debug.WriteLine still nothing in the output window.Reisch
Can you instead use Google Test as described [in this question][1]? [1]: #16531898Klecka
S
19

You can use Debug::WriteLine() (in the System::Diagnostics namespace) or Console::WriteLine() to write output to the Visual Studio 2012 console.

Code for the test (note that the System::Diagnostics namespace is declared elsewhere). The Test

The test result view.

enter image description here

After clicking the "Output" link:

enter image description here

It is not using std::cout, but hopefully this will do what you need it to do.

Schild answered 10/7, 2013 at 16:17 Comment(6)
Is there any way to watch it live? It's possible in NUnit.Charged
@Charged - I am not aware of a way to watch this sort of output live. A good option to watch live output uses OutputDebugString() and is described in this answer: https://mcmap.net/q/193482/-how-do-i-print-to-the-debug-output-window-in-a-win32-appSchild
THANK GOD! Jesus, you wouldn't believe how hard it is to find this answer. Thank you.Watery
@Schild sometimes the output appears, sometimes it doesn't, I'm not sure why? any suggestions to fix, I'm using vs enterprise rc. I tried both Console and Debug WritelineSecretion
@Secretion - Since it does show intermittently for you, it makes me wonder if in some cases, the Console::WriteLine() or Debug::WriteLine() is on a code path within your test that is not being executed. You could attempt replacing the logging line with Assert::Fail("I failed here") or similar. If the test doesn't fail with this message, then that line is not executing. Apologies if this seems too obvious, but sometimes it is simpler to check the obvious and impossible cases first.Schild
@Schild The asserts execute, sometimes when It's really necessary I manually fail the assert in order to see output, for instance Assert.IsNotNull(null, result); concatenating output with error message, I employed file write as well but is cumbersome, so the point is it does execute but output from Console or Debug is not 100% reliable in my system, is there some kind of setting I could check on or off?Secretion
M
17

For me seems to work using:

Logger::WriteMessage("What ever message");

After you run the test you can see the output in the Test Explorer window by clicking on output

Mirza answered 15/9, 2013 at 14:22 Comment(4)
I had to change the "Show output from:" from "General" to "Tests" on the Output window (Ctrl + W, O).Denbighshire
This seems to be the only way in the "native" tests. thanks.Aquarelle
what reference is needed to make Logger available?Pupillary
@MatthewJamesBriggs That's in C++ not in C#, so i don't think there's any references.Mirza
F
2

I don't know that I can give you a definitive answer, but I may be able to provide a clue.

In my older code that needed to get output to the console window during a custom build step, I used the following lines:

_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);

There is a description at http://msdn.microsoft.com/en-us/library/8hyw4sy7(v=vs.71).aspx for _CrtDbgReport.

For me, this made the output from my managed C++ show up through the build output window. Hope it can help you with Unit Testing.

Feticide answered 10/7, 2013 at 19:45 Comment(0)
R
1

According to Microsoft connect trx and test results are deprecated

:(

http://connect.microsoft.com/VisualStudio/feedback/details/750184/test-results-window-does-not-show-test-results

Reisch answered 3/7, 2013 at 17:35 Comment(2)
Although there is a connect ticket declaring this isn't supported I do see my debugging info in the output window. Not sure why it works intermittentlyReisch
Even though this question was for C++. I am explicitly asking this question for C#. The answer is it sometimes doesn't work. I am not sure why. But it does work...Reisch

© 2022 - 2024 — McMap. All rights reserved.