You still need an integration test, not unit, as others have correctly suggested.
Integration test
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;
}
}
Implementation
Program.cs
Console.Write($"Welcome, John!");