NUnit - 如何测试实现特定接口的所有类
如果我有接口 IFoo,并且有几个实现它的类,那么针对该接口测试所有这些类的最佳/最优雅/最聪明的方法是什么?
我想减少测试代码重复,但仍然“忠于”单元测试的原则。
您认为最佳实践是什么? 我正在使用 NUnit,但我认为任何单元测试框架中的示例都是有效的
If I have interface IFoo, and have several classes that implement it, what is the best/most elegant/cleverest way to test all those classes against the interface?
I'd like to reduce test code duplication, but still 'stay true' to the principles of Unit testing.
What would you consider best practice? I'm using NUnit, but I suppose examples from any Unit testing framework would be valid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您有类实现任何一个接口,那么它们都需要实现该接口中的方法。 为了测试这些类,您需要为每个类创建一个单元测试类。
让我们走一条更聪明的路线吧; 如果您的目标是避免代码和测试代码重复,您可能需要创建一个抽象类来处理重复出现代码。
例如,您有以下接口:
您可能想要创建一个抽象类:
测试很容易; 在测试类中实现抽象类作为内部类:
...或者让测试类扩展抽象类本身(如果符合您的喜好)。
使用抽象类来处理接口隐含的公共代码可以提供更清晰的代码设计。
我希望这对你有意义。
附带说明一下,这是一种常见的设计模式,称为模板方法模式< /强>。 在上面的示例中,模板方法是
CommonCode
方法,而SpecificCode
称为存根或挂钩。 这个想法是任何人都可以扩展行为而无需了解幕后的东西。许多框架都依赖于这种行为模式,例如 ASP.NET如果您必须在页面或用户控件中实现挂钩,例如由
Load
事件调用的生成的Page_Load
方法,则模板方法会调用场景。 这方面的例子还有很多。 基本上,您必须使用“load”、“init”或“render”等词实现的任何内容都是由模板方法调用的。If you have classes implement any one interface then they all need to implement the methods in that interface. In order to test these classes you need to create a unit test class for each of the classes.
Lets go with a smarter route instead; if your goal is to avoid code and test code duplication you might want to create an abstract class instead that handles the recurring code.
E.g. you have the following interface:
You might want to create an abstract class:
Testing that is easy; implement the abstract class in the test class either as an inner class:
...or let the test class extend the abstract class itself if that fits your fancy.
Having an abstract class take care of common code that an interface implies gives a much cleaner code design.
I hope this makes sense to you.
As a side note, this is a common design pattern called the Template Method pattern. In the above example, the template method is the
CommonCode
method andSpecificCode
is called a stub or a hook. The idea is that anyone can extend behavior without the need to know the behind the scenes stuff.A lot of frameworks rely on this behavioral pattern, e.g. ASP.NET where you have to implement the hooks in a page or a user controls such as the generated
Page_Load
method which is called by theLoad
event, the template method calls the hooks behind the scenes. There are a lot more examples of this. Basically anything that you have to implement that is using the words "load", "init", or "render" is called by a template method.我不同意 Jon Limjap 当他说,
合约的许多部分可能没有在返回类型中指定。 一个与语言无关的示例:
您对 LinkedList、ArrayList、CircularlyLinkedList 和所有其他列表的单元测试不仅应该测试列表本身是否已返回,还应该测试它们是否已被正确修改。
有一个关于按合同设计的早期问题,这可以帮助您为干燥这些测试的一种方法指明正确的方向。
如果您不想要合同的开销,我建议测试设备,遵循Spoike 推荐:
I disagree with Jon Limjap when he says,
There could be many parts of the contract not specified in the return type. A language-agnostic example:
Your unit tests on LinkedList, ArrayList, CircularlyLinkedList, and all the others should test not only that the lists themselves are returned, but also that they have been properly modified.
There was an earlier question on design-by-contract, which can help point you in the right direction on one way of DRYing up these tests.
If you don't want the overhead of contracts, I recommend test rigs, along the lines of what Spoike recommended:
我认为这不是最佳实践。
简单的事实是,接口只不过是实现方法的契约。 它不是 a.) 该方法应该如何实现和 b.) 该方法应该准确执行什么操作(它只保证返回类型)的契约,这是我收集的两个原因成为您想要进行此类测试的动机。
如果您确实想控制方法实现,则可以选择:
:
任何正确引用该扩展方法的实现都将精确地发出该扩展方法,因此您只需测试它一次。
I don't think this is best practice.
The simple truth is that an interface is nothing more than a contract that a method is implemented. It is not a contract on either a.) how the method should be implemented and b.) what that method should be doing exactly (it only guarantees the return type), the two reasons that I glean would be your motive in wanting this kind of test.
If you really want to be in control of your method implementation, you have the option of:
Example:
Any implementation properly referencing to that extension method will emit precisely that extension method so you only need to test it once.
[TestFixture]类的层次结构怎么样? 将公共测试代码放在基测试类中,并将其继承到子测试类中。
How about a hierarchy of [TestFixture]s classes? Put the common test code in the base test class and inherit it into child test classes..
当测试接口或基类契约时,我更喜欢让测试框架自动查找所有实现者。 这使您可以专注于被测试的接口,并合理地确定所有实现都将被测试,而无需进行大量的手动实现。
CombinatorialTest
参数上带有UsingImplementations
属性。除了测试接口的基础知识之外,您还应该测试每个单独的实现是否遵循其特定的要求。
When testing an interface or base class contract, I prefer to let the test framework automatically take care of finding all of the implementers. This lets you concentrate on the interface under test and be reasonably sure that all implementations will be tested, without having to do a lot of manual implementation.
CombinatorialTest
withUsingImplementations
attributes on the parameters.Beyond testing the basics of the interface, you should also test that each individual implementation follows its particular requirements.
我不使用 NUnit,但我测试过 C++ 接口。 我将首先测试一个 TestFoo 类,它是它的基本实现,以确保通用的东西可以工作。 然后你只需要测试每个界面特有的东西。
I don't use NUnit but I have tested C++ interfaces. I would first test a TestFoo class which is a basic implementation of it to make sure the generic stuff works. Then you just need to test the stuff that is unique to each interface.