Capturing Console.WriteLine output?

621 views
Skip to first unread message

Mike

unread,
Oct 18, 2008, 1:27:03 AM10/18/08
to NUnit-Discuss
Hello!

I'm currently trying to use the nunit-console test runner, and
attempting to capture the output of the tests that I'm running.
Specifically, I'm attempting to grab the output produced by
Console.WriteLine. It appears that the GUI version of the NUnit
runner will grab this and display the output in the gui, but I'm
looking to deal with the output programmatically.

Ideally, I'd like to capture the output within the test itself, so
that I could then use the Assert functionality to test that the actual
output matches what I'm expecting. If that's not possible, then I'd
like to direct the WriteLine output to a file, and then I'll be able
to deal with it in a separate program.

That all being said, I'm having trouble capturing the output. I
tried using the /out:Out.txt switch to the NUnit console runner (on
WinXP, using NUnit 2.4.8), and it only produces 0-byte files. It does
produce the file, it's just zero bytes.

I apologize if there's a simple fix, or something obvious that I'm
missing (which is usually what the problem is :) ), but if anyone
could confirm/deny that the /out switch should do what I think it
does, that would be great. If anyone happens to know how I could
capture this output in the test itself, that would be absolutely
wonderful. And if not, then I'll just keep working on it :)

Thanks!
--Mike

Charlie Poole

unread,
Oct 18, 2008, 1:53:41 AM10/18/08
to nunit-...@googlegroups.com
Hi Mike,

The /out switch should send any output that your tests
normally write to the console to a file - note that
it doesn't capture any other output, such as the
test summary info that NUnit writes.

However, it sounds like you want to capture output
from the program you are testing, rather than your
test, so you can assert on it. The easiest way to
do that is to redirect Console.Out in your test
to a stringwriter.

Charlie

Brad Stiles

unread,
Oct 18, 2008, 11:07:16 AM10/18/08
to nunit-...@googlegroups.com
> Ideally, I'd like to capture the output within the test itself, so
> that I could then use the Assert functionality to test that the
> actual output matches what I'm expecting. If that's not possible,
> then I'd like to direct the WriteLine output to a file, and then
> I'll be able to deal with it in a separate program.

Mike,

When I've had to deal with this problem, I usually resort to either
overriding System.Console.Out to a file or string writer, or I write the
method or object so that it accepts a stream or textwriter to which I
can write, and inject a test or mock one into the class at test time.

Brad

Mike

unread,
Oct 18, 2008, 3:38:14 PM10/18/08
to NUnit-Discuss
Thank you both for your help with this! I greatly appreciate it!

Charlie: Thanks for the clarification about the /out switch: now that
I'm clear about what this does (and doesn't) do, I won't spend any
more time trying to "get it to work" :)

Both - thanks a bunch for the pointer about the StringWriter trick! I
tried using Console.SetOut to capture the output to a string writer,
and it looks like it's doing exactly what I want - I'll be able to
collect up the output at the end of the test, and figure out if it
matches what I'm expecting.

Thanks a bunch for help!
--Mike
Reply all
Reply to author
Forward
0 new messages