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.

2
  • If I were to use Console.WriteLine or Debug.WriteLine still nothing in the output window. Commented Jul 3, 2013 at 17:11
  • Can you instead use Google Test as described [in this question][1]? [1]: stackoverflow.com/questions/16531398/… Commented Jul 8, 2013 at 0:15

4 Answers 4

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.

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

6 Comments

Is there any way to watch it live? It's possible in NUnit.
@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: stackoverflow.com/a/1333542/542494
THANK GOD! Jesus, you wouldn't believe how hard it is to find this answer. Thank you.
@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 - 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.
|
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

4 Comments

I had to change the "Show output from:" from "General" to "Tests" on the Output window (Ctrl + W, O).
This seems to be the only way in the "native" tests. thanks.
what reference is needed to make Logger available?
@MatthewJamesBriggs That's in C++ not in C#, so i don't think there's any references.
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.

Comments

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

2 Comments

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

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.