单元测试帮助。如何测试控制台的消息输出?

发布于 2024-08-02 14:25:35 字数 839 浏览 2 评论 0原文

我是单元测试的新手。如何检查控制台输出? 我有

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);
        }
    }


}

我应该如何正确地测试这个? 感谢您的指点。 干杯, 〜ck

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

屌丝范 2024-08-09 14:25:35

字面上的答案是,在调用被测试的类之前,您将使用Console.SetOut将stdout定向到内存流或类似的内容,您可以稍后检查其内容。

更好的答案是使用模拟框架(例如 Rhino Mocks)来创建抽象类的具体实例,并期望调用 DrawXXShape 方法。

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.

雨巷深深 2024-08-09 14:25:35

您不需要测试“Console.WriteLine”例程,因为您必须假设它有效 - 它不是您的代码,所以为什么要测试它。您需要测试是否生成传递给“Console.WriteLine”的正确字符串。

换句话说,

public override void DrawXXShape()
{
    Console.WriteLine("The XXCircle was drawn.");
}

您可以这样做:而不是:

public override void DrawXXShape()
{
    Console.WriteLine(produceXxCircle());
}

public string produceXxCircle()
{
    return "The XXCircle was drawn.";
}

然后在测试用例中:

Assert.AreEqual(produceXxCircle(), "The XXCircle was drawn.");

希望它有帮助。
问候
西蒙

You don't need to test 'Console.WriteLine' routine because you have to assume it works - it is not your code, so why do you want to test it. You need to test whether you produce correct string that is passed to 'Console.WriteLine'

In other words, instead of:

public override void DrawXXShape()
{
    Console.WriteLine("The XXCircle was drawn.");
}

you could do:

public override void DrawXXShape()
{
    Console.WriteLine(produceXxCircle());
}

public string produceXxCircle()
{
    return "The XXCircle was drawn.";
}

and then in the test case:

Assert.AreEqual(produceXxCircle(), "The XXCircle was drawn.");

Hope it helps.
Regads
Simon

被翻牌 2024-08-09 14:25:35

那根本不是你会做的。

在测试中,您通常会检查对象的状态,例如:

Assert.IsTrue(foo.Field, Equals, "hello")

取决于您选择的框架的工作方式。因此,一般来说,您需要改变您的方法以符合此模型。

That's not at all what you'd do.

In your test you will typically check for the state of the object, with something like:

Assert.IsTrue(foo.Field, Equals, "hello")

Depending on how your chosen framework works. So you'll need to change your approach, in general, to conform to this model.

半岛未凉 2024-08-09 14:25:35

我假设其他一些测试测试绘图功能 - 如果您现在想测试您的类是否向控制台写入了一些特定的内容,那么您应该抽象出写入控制台的想法。

创建一个包含 WriteLine() 方法的接口,并将实现该接口的实例注入到 XXShapes 中。您的测试可以注入模拟或存根,它们可以捕获写入的字符串并在测试中测试其内容。

I'm assuming that some other tests test for the drawing capabilities - If you now want to test that your classes write something in particular to the console, then you should abstract the idea of writing to the console.

Create an interface with a WriteLine() method in it and inject instances that implement this interface into XXShapes. Your tests can inject mocks or stubs which can capture the strings that are written and test their contents in the tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文