Reputation: 391556
I am building a unit test in C# with NUnit, and I'd like to test that the main program actually outputs the right output depending on the command line arguments.
Is there a way from an NUnit test method that calls Program.Main(...)
to grab everything written to Console.Out
and Console.Error
so that I can verify against it?
Upvotes: 60
Views: 28475
Reputation: 7396
You still need an integration test, not unit, as others have correctly suggested.
ProgramTest.cs
using NUnit.Framework;
class ConsoleTests
{
[Test]
public void TestsStdOut()
{
var capturedStdOut = CapturedStdOut(() =>
{
RunApp();
});
Assert.AreEqual("Welcome, John!", capturedStdOut);
}
void RunApp(string[]? arguments = default)
{
var entryPoint = typeof(Program).Assembly.EntryPoint!;
entryPoint.Invoke(null, new object[] { arguments ?? Array.Empty<string>() });
}
string CapturedStdOut(Action callback)
{
TextWriter originalStdOut = Console.Out;
using var newStdOut = new StringWriter();
Console.SetOut(newStdOut);
callback.Invoke();
var capturedOutput = newStdOut.ToString();
Console.SetOut(originalStdOut);
return capturedOutput;
}
}
Program.cs
Console.Write($"Welcome, John!");
Upvotes: 1
Reputation: 233277
You can redirect Console.In
, Console.Out
and Console.Error
to custom StringWriters, like this
[TestMethod]
public void ValidateConsoleOutput()
{
using (StringWriter sw = new StringWriter())
{
Console.SetOut(sw);
ConsoleUser cu = new ConsoleUser();
cu.DoWork();
string expected = string.Format("Ploeh{0}", Environment.NewLine);
Assert.AreEqual<string>(expected, sw.ToString());
}
}
See this blog post for full details.
Upvotes: 93
Reputation: 1867
You can use this simple class to get the output with a using statement:
public class ConsoleOutput : IDisposable
{
private StringWriter stringWriter;
private TextWriter originalOutput;
public ConsoleOutput()
{
stringWriter = new StringWriter();
originalOutput = Console.Out;
Console.SetOut(stringWriter);
}
public string GetOuput()
{
return stringWriter.ToString();
}
public void Dispose()
{
Console.SetOut(originalOutput);
stringWriter.Dispose();
}
}
Here is an example how to use it:
using (var consoleOutput = new ConsoleOutput())
{
target.WriteToConsole(text);
Assert.AreEqual(text, consoleOutput.GetOuput());
}
you can find more detailed information and a working code sample on my blog post here - Getting console output within a unit test.
Upvotes: 23