首页 > 解决方案 > 将库测试中的测试接口导入应用程序的测试

问题描述

我做了一个图书馆。有序集合。我在应用程序中导入和使用。库中有一个junit5测试接口。我想在应用程序中使用测试接口。

例如,在图书馆你有

interface BinaryTree<K:Comparable<K>,V> : Collection<Pair<K,V> {...}

在你拥有的这个库的测试中

interface TreeTests<K : Comparable<K>, V> {
    val tree: BinaryTree<K, V>
    @Test
    fun sorted() {
        val list = tree.toList().sortedBy { it.first }
        assertIterableEquals(list, tree)
    } 
}

我在 BinaryTree 的应用程序中做了一个实现,但它是由 sqlite 支持的。我想在应用程序的实现上使用库中的测试接口。像这样

class TreeDB:BinaryTree<Int,String>(){...}

在应用程序的测试中

class TreeDBTests:TreeTests<Int,String> {
   override val tree = TreeDB()
}

然后为 BinaryTree 的可能实现编写的所有测试都在 TreeDB 类上运行。

但是,在库的测试中编写的代码不会导出到库的使用者。无论如何要使这项工作?

标签: kotlinjunit5

解决方案


我建议将测试工具视为任何其他有目的的库。如果在您的情况下似乎还需要这些测试工具来测试原始库,则您必须将接口和实现与原始库分开,例如

bintree-interface <– bintree-testutils (TreeTests etc.)
                ^– bintree-implementation 

现在bintree-implementationcan useTreeTests的测试和你的TreeDBcan 测试也是如此。


推荐阅读