简体   繁体   English

如何在具有不同依赖关系的模块之间重复一组测试?

[英]How do I repeat a set of tests across modules with different dependencies?

I'm starting a project where a "core" module will define a set of common interfaces and other modules implement those interfaces using different libraries on the back end. 我正在开始一个项目,其中“核心”模块将定义一组公共接口,而其他模块使用后端的不同库来实现这些接口。 It's very similar to SLF4J, where the API module describes all the operations you can use and the binding modules implement those operations in various logging frameworks like Log4j, java.util logging, and Logback. 它与SLF4J非常相似,其中API模块描述了您可以使用的所有操作,绑定模块在各种日志框架(如Log4j,java.util日志记录和Logback)中实现这些操作。 To use it, you depend on the API module and one of the binding modules, and you only ever interact with the API. 要使用它,您需要依赖API模块和其中一个绑定模块,并且您只能与API进行交互。

My problem lies in testing. 我的问题在于测试。 Since the API expresses all possible operations and every binding module should behave the same way, I want to write one set of tests that describes the expected behavior and run the same tests against every binding module. 由于API表示所有可能的操作,并且每个绑定模块应该以相同的方式运行,因此我想编写一组描述预期行为的测试,并针对每个绑定模块运行相同的测试。 How should I organize things to get this to work without a lot of repeated effort? 如何在不经过多次努力的情况下组织工作以实现这一目标?

This is a Java project with plenty of Groovy, and I plan to build it with Gradle. 这是一个包含大量Groovy的Java项目,我打算用Gradle构建它。

i've done this sort of thing with JUnit before. 我以前用JUnit做过这种事。 what i did was define a set of abstract test classes with all the logic: 我所做的是用所有逻辑定义一组抽象测试类:

public abstract AbstractTestBase {
    protected abstract CommonInterface getInstance(); //for subclasses to implement

    @Test
    public void testSomething() {
       CommonInterface instance = getInstance();
       //test instance
    }
}

and then concrete subclasses are usually just tiny classes that return the concrete implementation to be tested: 然后具体的子类通常只是很小的类,它们返回要测试的具体实现:

public class TestSomeImplementation extends AbstractTestBase {
    protected CommonInterface getInstance() {
        return new SomeImplementation();
    }
}

on some i've added specific tests for specific implementations, but in your case i understand you wont need to. 在某些我已经为特定的实现添加了特定的测试,但在你的情况下我明白你不需要。

It depends on the binding mechanism. 这取决于绑定机制。 If multiple bindings can co-exist, you can tackle the problem on the test level, by writing a data-driven test that runs the same test class/method for each binding. 如果多个绑定可以共存,则可以通过编写针对每个绑定运行相同测试类/方法的数据驱动测试来解决测试级别上的问题。 (Details on how to achieve this depend on the test framework in use.) If bindings cannot co-exist, you can tackle the problem on the build level. (有关如何实现此目的的详细信息取决于使用的测试框架。)如果绑定不能共存,则可以在构建级别上解决问题。 For example, you could create ten Test tasks which would be configured identically except for the implementation Jar on their class path. 例如,您可以创建十个Test任务,除了类路径上的实现Jar之外,这些任务的配置相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在控制器测试中注入依赖项? - How do I inject dependencies in controller tests? 如何解析作为模块且在 POM 中没有 relativePath 的依赖项? - How do I resolve dependencies which are modules and have no relativePath in the POM? 如何在pom中设置不编译测试? - How do I set in the pom to not compile tests? 如何处理多个guice私有模块之间的循环依赖关系? - How to handle circular dependencies across multiple guice private modules? 如何用假测试模块替换 Guice 模块进行单元测试? - How do I substitute Guice modules with fake test modules for unit tests? 如何构建2个具有不同依赖关系的jar文件? - How do I build 2 jar files with different dependencies? 我如何打开包并要求依赖于仅用于 JUnit 测试的测试范围模块 - How do I open packages and require dependencies on test scope modules only for JUnit testing 如何使用不同的测试值执行两个测试? - How do I perform two tests with different test values? 如何在两种不同的文件格式上运行单元测试? - How do I run unit tests on two different file formats? 有没有办法跨越TestNG中的Tests依赖关系? - Is there way to span dependencies across Tests in TestNG?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM