如何为运行(Cucumber)验收测试指定单独的maven目标?

问题描述 投票:8回答:2

我有以下项目结构:

MyProject
   --src
   --test
      --acceptance
         --step_definitions
         --features
      --unit

我希望能够在测试/单元中声明的单元测试中单独在Maven中运行我的黄瓜测试(在测试/验收中),以便它们可以在不同的CI构建计划中运行等。我正在使用cucumber-junit所以每个验收测试的“跑步者”都是用JUnit编写的。

这可能吗?

maven integration-testing cucumber-jvm cucumber-junit
2个回答
13
投票

这可能吗?

对的,这是可能的。我相信你应该将你的单位与接受/整合测试分开:

稍微修改了这两个文件夹的结构,将集成测试文件放在standard locationsrc/it中:

MyProject/

  • src/main/java/(如何)
  • src/test/(单元测试代码) java/ resources/
  • src/it/(验收/整合测试) java/(步骤定义) resources/(功能文件)

此外,通过设计,不同的Maven插件用于单元和集成测试:

你还必须bind execution of maven-failsafe-pulgin。要单独运行集成测试,您可以定义新的配置文件:

<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>

对于测试用例,您还需要configure the plugin to search src/it目录树。

接受测试可以在以后使用:

mvn clean verify -Pacceptance-tests

如需完整样本,我建议您关注http://www.weblogism.com/item/334/integration-tests-with-cucumber-jvm-selenium-and-maven


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。这意味着您需要为测试命名策略以将它们彼此分开。

© www.soinside.com 2019 - 2024. All rights reserved.