我有两个不同的类,它们共享一个公共(public)接口(interface)。尽管功能相同,但它们内部的工作方式却截然不同。所以我很自然地想测试它们。
The best example I can come up with; I serialize something to a file, one class serialize it to plaintext, the other to xml. The data (should) look the same before and after the serialization regardless of method used.
以相同方式测试这两个类的最佳方法是什么?这些测试仅在实例化不同类的方式上有所不同。我不想复制整个测试、重命名并更改一行。
测试目前在 JUnit 中,但无论如何我都会将它们移植到 NUnit,所以代码并不重要。我更寻找适用于该测试套件的设计模式。
最佳答案
为测试创建一个公共(public)抽象基测试类。
abstract class BaseTest{
@Test
public void featureX(){
Type t = createInstance();
// do something with t
}
abstract Type createInstance();
}
ConcreteTest extends BaseTest{
Type createInstace(){
return //instantiate concrete type here.
}
}
关于c# - 将一项测试应用于两个不同的类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1741210/