我有以下项目结构:
MyProject
--src
--test
--acceptance
--step_definitions
--features
--unit
我希望能够在测试/单元中声明的单元测试中分别在Maven中运行我的黄瓜测试(在测试/验收中),以便它们可以在不同的CI构建计划中运行等。我正在使用黄瓜-junit所以每个验收测试的'runners'都是用JUnit编写的。
这可能吗?
答案 0 :(得分:11)
这可能吗?
是的,有可能。我相信你应该将你的单位与接受/整合测试分开:
稍微已修改的文件夹结构,这两种方法都将您的集成测试文件放在src/it
MyProject/
中:
src/main/java/
src/test/
(SUT)java/
(单元测试代码)
resources/
src/it/
java/
(接受/整合测试)
resources/
(步骤定义)<profiles>
<profile>
<id>acceptance-tests</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
(功能文件)此外,根据设计,不同的Maven插件适用于单元和集成测试:
maven-surefire-plugin
您还必须maven-failsafe-plugin
。要单独运行集成测试,您可以定义新的配置文件:
src/it
对于测试用例,您还需要bind execution of maven-failsafe-pulgin
mvn clean verify -Pacceptance-tests
目录树。
验收测试可以在以后使用:
运行{{1}}
如需完整样本,建议您按照configure the plugin to search
进行操作答案 1 :(得分:2)
The other answer建议修改您的文件夹结构,使其具有用于集成和验收测试的共享文件夹,但您也可以拥有原始文件夹结构。你还想在评论中提到你要将这三个(包括未提及的集成测试)分开,这是可能的,虽然是hackish。
由于您似乎有test/unit
用于单元测试而test/acceptance
用于验收测试,因此我假设test/integration
进行集成测试。
<profiles>
<profile>
<id>acceptance-test</id>
<build>
<plugins>
<plugin>
<!-- to run directly: mvn failsafe:integration-test -P acceptance-test -->
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<configuration>
<testSourceDirectory>test/acceptance</testSourceDirectory>
<includes>
<include>**/*Acceptance.java</include>
</includes>
<excludes>
<exclude>**/*IT.java</exclude>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>test/unit</source>
<source>test/integration</source>
<source>test/acceptance</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<testSourceDirectory>test/unit</testSourceDirectory>
</configuration>
</plugin>
<plugin>
<!-- to run directly: mvn failsafe:integration-test -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<testSourceDirectory>test/integration</testSourceDirectory>
</configuration>
<!-- execution below can be used, if tests are needed on
mvn:integration-test -->
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
请注意,分离仅适用于源:已编译的文件将全部转到同一文件夹,AFAIK that's something you can't change。这意味着您需要为测试命名策略以将它们彼此分开。