I am a newbie to unit testing. How do I check the for console output? I have
namespace XXShapes
{
public abstract class XXShape
{
public virtual void DrawXXShape()
{
Console.WriteLine("The XXShape was drawn.");
}
}
public class XXCircle : XXShape
{
public override void DrawXXShape()
{
Console.WriteLine("The XXCircle was drawn.");
}
}
}
namespace XXShapes.Test
{
[TestFixture]
public class XXShapeTest
{
[Test]
public void MyFirstTest()
{
XXShape s = new XXCircle();
string expected = "The XXCircle was drawn.";
s.DrawXXShape();
string actual = Console.ReadLine();
Assert.AreEqual(expected, actual);
}
}
}
How should I correctly be testing this? Thanks for any pointers. Cheers, ~ck
In the main menu of VS, choose View > Output, then within the Output pane, pick "Tests".
Run unit tests Run your unit tests by clicking Run All (or press Ctrl + R, V).
The literal answer would be that you would use Console.SetOut
before calling the class under test to direct stdout
into a memoryStream or similar, whose contents you can later inspect.
The better answer would be to use a mocking framework, like Rhino Mocks to create a concrete instance of your abstract class, with an expectation set that the DrawXXShape
method would be called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With