我可以实现一系列的可重复使用的测试来测试接口的实现? [英] Can I implement a series of reusable tests to test an interface's implementation?

查看:161
本文介绍了我可以实现一系列的可重复使用的测试来测试接口的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一系列在C#集合类,每个实施类似的自定义接口。是否可以编写一个集合的单元测试的接口,并自动在几个不同的实现运行它们呢?我想,以避免任何重复测试code每个实现。

I am writing a series of collection classes in C#, each of which implement similar custom interfaces. Is it possible to write a single collection of unit tests for an interface, and automatically run them all on several different implementations? I would like to avoid any duplicated testing code for each implementation.

我愿意考虑任何框架(NUnit的等)或Visual Studio扩展做到这一点。

I'm willing to look into any framework (NUnit, etc.) or Visual Studio extension to accomplish this.

对于那些希望做同样的,我贴我的具体的解决方案,基于关 avandeursen的接受的解决方案,因为< A HREF =htt​​p://stackoverflow.com/a/9382920/343238>答案。

For those looking to do the same, I posted my concrete solution, based off of avandeursen's accepted solution, as an answer.

推荐答案

是的,这是可能的。诀窍就是让你的单元测试的类层次跟随你的code中的类层次结构。

Yes, that is possible. The trick is to let your unit class test hierarchy follow the class hierarchy of your code.

让我们假设你有一个接口 ITF 与实现类 C1 C2

Let's assume you have an interface Itf with implementing classes C1 and C2.

您先创建一个测试类 ITF ItfTest )。要实际行使的测试,你需要创建一个模拟实现你的 ITF 接口。

You first create a test class for Itf (ItfTest). To actually exercise the test, you need to create a mock implementation of your Itf interface.

在此所有测试 ItfTest 应通过任何实施 ITF 的(!)。如果没有,你实现不符合里氏替换原则(以下简称L ://davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html相对=nofollow> OO设计的SOLID 原则)

All tests in this ItfTest should pass on any implementation of Itf (!). If not, your implementation does not conform to the Liskov Substitution Principle (the "L" in Martin's SOLID principles of OO design)

因此​​,创建一个测试案例 C1 ,您的 C1Test 类可以扩展 ItfTest 。您的扩展应该通过创建一个 C1 对象(在注射,或使用的 GoF的工厂方法)。这样一来,所有的 ItfTest 情况下应用于类型的实例 C1 。此外,你的 C1Test 类可以包含具体到 C1额外的测试用例

Thus, to create a test case for C1, your C1Test class can extend ItfTest. Your extension should replace the mock object creation with the creation of a C1 object (injecting it in, or using a GoF factory method). In this way, all ItfTest cases are applied to instances of type C1. Furthermore, your C1Test class can contain additional test cases specific to C1.

同样,对于 C2 。你可以重复的伎俩更深层次的嵌套类和接口。

Likewise for C2. And you can repeat the trick for deeper nested classes and interfaces.

参考:活页夹的<一个href="http://books.google.nl/books?id=P3UkDhLHP4YC&pg=PA513&lpg=PA513&dq=robert+binder+polymorphic+server+test&source=bl&ots=_Efn5sbTG4&sig=cE9IGulcQ34IlZpOlYtcJ0l9nPc&hl=en&sa=X&ei=MKtCT9n3D6HX0QXSqZCPDw&redir_esc=y#v=onepage&q=robert%20binder%20polymorphic%20server%20test&f=false"相对=nofollow>多态性服务器测试模式, 与麦格雷戈的 PACT - 并行架构组件测试

References: Binder's Polymorphic Server Test pattern, and McGregor's PACT -- Parallel Architecture for Component Testing.

这篇关于我可以实现一系列的可重复使用的测试来测试接口的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆