单元测试帮助。如何测试控制台的消息输出?
我是单元测试的新手。如何检查控制台输出? 我有
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
字面上的答案是,在调用被测试的类之前,您将使用Console.SetOut将stdout定向到内存流或类似的内容,您可以稍后检查其内容。
更好的答案是使用模拟框架(例如 Rhino Mocks)来创建抽象类的具体实例,并期望调用
DrawXXShape
方法。The literal answer would be that you would use
Console.SetOut
before calling the class under test to directstdout
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.您不需要测试“Console.WriteLine”例程,因为您必须假设它有效 - 它不是您的代码,所以为什么要测试它。您需要测试是否生成传递给“Console.WriteLine”的正确字符串。
换句话说,
您可以这样做:而不是:
然后在测试用例中:
希望它有帮助。
问候
西蒙
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:
you could do:
and then in the test case:
Hope it helps.
Regads
Simon
那根本不是你会做的。
在测试中,您通常会检查对象的状态,例如:
取决于您选择的框架的工作方式。因此,一般来说,您需要改变您的方法以符合此模型。
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:
Depending on how your chosen framework works. So you'll need to change your approach, in general, to conform to this model.
我假设其他一些测试测试绘图功能 - 如果您现在想测试您的类是否向控制台写入了一些特定的内容,那么您应该抽象出写入控制台的想法。
创建一个包含 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.