如何在其他IT中重用集成测试的类?

时间:2016-06-16 17:28:07

标签: maven

我在我的maven项目中有几个集成测试,它们具有从一个IT复制到另一个IT的公共类,很明显它不是一种干净的方法。所以我想知道是否有办法在其他IT中重用给定IT的类。

至于我的IT工件没有在我的本地存储库中发布,我发现的唯一方法是在我的主类中移动公共类但是它仍然非常难看,因为我将主类与测试类混合以避免复制测试代码。

这是我的项目结构:

resources
src
└── it
|   └── it1
|   |   └── src
|   |       └── main
|   └── it2
|   |   └── src
|   |        └── main
... ...
└── main
    └── java
    └── resources
    test
    └── java
    └── resources

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案是创建一个新的通用集成测试,除了包含公共类之外什么都不做。您可以prepare the build environment确保在使用setupIncludes参数的其他人之前执行并安装在本地存储库中的常见IT测试。

  

通常,插件假定要构建的项目彼此无关,因此它们的构建顺序无关紧要。但是,您有时可能希望确保某些项目在其他项目之前构建。作为动机,假设项目为其他项目执行某种常见设置,例如将实用工件安装到本地存储库中。

默认情况下,这会匹配名为setup*/pom.xml的任何项目,因此我们可以将其命名为setup-its

src
+--it
    +--setup-its
        +--invoker.properties
        +--pom.xml
    +--it1
        +--pom.xml
    +--it2
        +--pom.xml

setup-its项目将包含公共类。您可以使用以下maven-invoker-plugininvokerPropertiesFile)告诉用于启动集成测试的invoker.properties将其安装到本地存储库中:

invoker.goals = clean install

然后,您可以依赖其他IT中的此工件,就像您对任何工件一样。

setup-its

的示例
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>setup-its</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</project>

it1

的示例
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>it1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>test</groupId>
            <artifactId>setup-its</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

当有一个项目为其余项目设置环境时,这种方法很有效。如果需要进一步控制构建顺序,则必须依赖于创建具有内部依赖性的多模块项目。