c# - 将一项测试应用于两个不同的类(class)

标签 c# java unit-testing design-patterns junit

我有两个不同的类,它们共享一个公共(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/

相关文章:

基于 fork() 的 C++ 单元测试

c# - 在 Xamarin.Forms 中,如果您无法从 XAML 实例化 BindingContext,如何允许自动完成 XAML 代码中的绑定(bind)?

c# - linq query Take() 优先于 Distinct()?

java - Java中Semaphore的图书管理员资源分配问题

java - 处理多行字符串返回

Javascript - 单元测试中的模拟 FormData.entries()

python - 模拟跨库的调用链

c# - Visual Studio 2019 项目模板仅针对某些类型的项目显示

c# - 抑制对象终结,直到其他线程运行

java - 使用 android 的默认图库 Intent