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.

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
  • If I were to use Console.WriteLine or Debug.WriteLine still nothing in the output window. – n8CodeGuru Jul 03 '13 at 17:11
  • Can you instead use Google Test as described [in this question][1]? [1]: http://stackoverflow.com/questions/16531398/how-can-i-add-a-unit-test-to-a-c-console-program-in-visual-studio-2012 – MichaelH Jul 08 '13 at 00:15

4 Answers4

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.

olen_garn
  • 902
  • 7
  • 16
  • 2
    Is there any way to watch it live? It's possible in NUnit. – Babak Jan 13 '14 at 10:44
  • 1
    @Babak - 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: http://stackoverflow.com/a/1333542/542494 – olen_garn Feb 05 '14 at 18:45
  • THANK GOD! Jesus, you wouldn't believe how hard it is to find this answer. Thank you. – Drew May 06 '16 at 17:42
  • @olen_garn 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 Writeline – user2727195 Feb 03 '17 at 15:37
  • 1
    @user2727195 - 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. – olen_garn Feb 03 '17 at 19:29
  • @olen_garn 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? – user2727195 Feb 04 '17 at 02:34
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

Hanan
  • 1,169
  • 3
  • 23
  • 40
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.

Prof Von Lemongargle
  • 3,658
  • 31
  • 29
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

n8CodeGuru
  • 413
  • 1
  • 6
  • 13
  • 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 intermittently – n8CodeGuru Jul 09 '13 at 13:33
  • 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... – n8CodeGuru Jul 10 '13 at 20:31