你如何对接口进行单元测试? [英] How do you unit test an interface?

查看:892
本文介绍了你如何对接口进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,有一个接口 IMyInterface ,并且有三个类支持这个接口:

For example, there is a interface IMyInterface, and three classes support this interface:

class A : IMyInterface
{
}

class B : IMyInterface
{
}

class C : IMyInterface
{
}

以最简单的方式,我可以写三个测试类:ATest,BTest,CTest并分别测试它们。但是,由于它们支持相同的接口,因此大多数测试代码都是相同的,很难维护。 如何使用简单易用的方法测试不同类支持的接口?

In the simplest way, I could write three test class : ATest, BTest, CTest and test them separately. However, since they support the same interface, most test code would be the same, it's hard to maintain. How can I use a simple and easy way to test a interface that is supported by different class?

以前在MSDN论坛上提出的问题

推荐答案

要测试具有常见测试的接口,无论实现如何,您都可以使用抽象测试用例,然后创建测试用例的具体实例对于接口的每个实现。

To test an interface with common tests regardless of implementation, you can use an abstract test case, and then create concrete instances of the test case for each implementation of the interface.

抽象(基础)测试用例执行实现中立测试(即验证接口契约),而具体测试负责实例化要测试的对象,并执行任何特定于实现的测试。

The abstract (base) test case performs the implementation-neutral tests (i.e. verify the interface contract) while the concrete tests take care of instantiating the object to test, and perform any implementation-specific tests.

这篇关于你如何对接口进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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