8

I am trying to show some information from a [TestMethod] method.

Usually we use NUnit and a line with Console.WriteLine runs fine and we can see it in 'output' window, but on this project we must to use Testing tools embebed with VS2010 and Console.WriteLine doesn't run because we cannot see anything.

hello B
  • 207
  • 1
  • 2
  • 2
  • Have you tried [`Debug.WriteLine()`](http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.writeline.aspx)? – svick Mar 09 '12 at 15:14
  • Have to say: you don't want to do that. The tests become slow, and someone has to watch that console. – Christian Horsdal Mar 10 '12 at 06:47
  • Hi @user6847, please mark an answer as the correct one, or edit your question to clarify why the question remains unanswered. – Peter Jun 26 '14 at 08:30

4 Answers4

13

OK, you have to use Assert and all that, but the other answers don't answer the actual question. Maybe you have your reasons (as I have mine, which is how I found this question).

This might help you a little:

It turns out to see a test’s output, you just double-click on the test summary line, and all the output is down at the bottom of that window. You get Console.Out messages and (more importantly) {Trace,Debug}.WriteLine()

If you're using ReSharper, select your test method in the Unit Test Sessions pane, and the output will be in the Output tab:

enter image description here

In my case, I just needed to quickly test some performance. As I already have a unit test project, it was quicker to do it this way than having to create a new Console Application. So instead of just telling people why their question is wrong, I believe we should tell them why their question is wrong, but still try to answer the question.

Sorry for the rant.

Peter
  • 998
  • 8
  • 20
  • 1
    This is a great response. Care about the community people - from the bottom to the top. – Anonymous Type Feb 26 '15 at 00:27
  • 1
    MAN this is difficult to find! Double-click, scroll down, click the tiny blue "output" link at the bottom. Thank you so much. – CindyH Mar 09 '16 at 16:20
10

I agree with the previous poster that if you need to verify something you assert it...

However... Your Console.WriteLine() message will show, after your test has completed double click the test result line in the Test Results tab, this will open the results for the individual test which contains a "Standard Console Output" section which has your Console.WriteLine() messages.

wessiyad
  • 219
  • 1
  • 7
3

Why do you need to "see anything"?

You should just use asserts to validate the test has worked correctly.

ozz
  • 8,322
  • 2
  • 29
  • 62
  • 1
    Agreed. If you're manually checking output, it isn't an automated test anymore! – Thorn G Mar 09 '12 at 15:39
  • A minus, and no comment, brilliant – ozz Mar 09 '12 at 16:36
  • 5
    Well, I wasn't the one who gave a -1, but I thought about it, because this answer, while it *may* be a good point, doesn't actually answer the question. – Eric King Mar 09 '12 at 17:11
  • 2
    It's one thing to answer the question, it's another to provide insight the asker didn't think of. It's the difference between giving someone what they want versus what they need. – Michael Brown Mar 09 '12 at 17:30
  • 4
    There's no reason you can't do both. – Eric King Mar 09 '12 at 17:32
  • @EricKing - don't get me wrong, I don't mind a minus if someone gives their reason. – ozz Mar 09 '12 at 21:49
  • 2
    If your code isn't working, the text of an output console can give you insight into why its not working. Asserts only allow you to test for things you have already thought of. – Greg Jun 26 '14 at 01:42
  • 1
    I agree with this answer. You should write `Assert` inside unit test methods. Manually checking output inside the visual studio's output window, is not the ideal way of unit testing coding practice. You can also debug unit test cases in visual studio if you want. I think it will solve your purpose. – Arnab Nov 16 '15 at 03:16
  • Sending 'information' to the console as the OP asked is not the same as verifying a test has passed. I think we would all agree that Asserts are the way to make sure the tests passes. As others have said, sending text to the output should be avoided where possible but the main reasons I have seen for sending text to the output are for reporting test metadata (e.g. platform/machine details) and for temporary debugging difficult issues with the tests (e.g. when a test passes when run on its own but fails when run with all the others). – Ben May 06 '20 at 16:24
  • Doesn't answer the question. This belongs in the comments imho. – Urasquirrel Jun 17 '21 at 18:14
  • Yeah, don't answer the question from 9 years ago! Wonder if the OP will be back 9 years later like me after seeing a notification for the above comment pop up! {;-) – ozz Jun 18 '21 at 08:47
2

You can set the standard output and input of Console to be a TextWriter / TextReader and perform asserts based on the content of those.

http://msdn.microsoft.com/en-us/library/system.console.setout.aspx

RobB
  • 51
  • 1
  • 4