Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Test Help. How do I test for a message output to console?

Tags:

c#

tdd

nunit

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

like image 208
Hcabnettek Avatar asked Aug 17 '09 07:08

Hcabnettek


People also ask

How do I view test output in Visual Studio?

In the main menu of VS, choose View > Output, then within the Output pane, pick "Tests".

How do you run a unit test in VS?

Run unit tests Run your unit tests by clicking Run All (or press Ctrl + R, V).


1 Answers

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.

like image 75
Steve Gilham Avatar answered Nov 01 '22 16:11

Steve Gilham