【问题标题】:How do I specify a separate maven goal for running (Cucumber) acceptance tests?如何为运行(Cucumber)验收测试指定一个单独的 Maven 目标?
【发布时间】:2013-08-10 17:34:10
【问题描述】:

我的项目结构如下:

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

我希望能够在 Maven 中与在 test/unit 中声明的单元测试分开运行我的黄瓜测试(在测试/验收中),以便它们可以在不同的 CI 构建计划等中运行。我正在使用黄瓜-junit,因此每个验收测试的“运行程序”都是用 JUnit 编写的。

这可能吗?

【问题讨论】:

标签: maven integration-testing cucumber-jvm cucumber-junit


【解决方案1】:

这可能吗?

是的,这是可能的。我认为您应该将您的单元与验收/集成测试分开:

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

MyProject/

  • src/main/java/ (SUT)
  • 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

【讨论】:

  • 最终决定使用 Gradle,因为这意味着我不必修改结构并且也可以将验收/集成测试分开。这对于我不得不使用 maven 的另一个项目确实很有帮助。
【解决方案2】:

The other answer 建议修改您的文件夹结构,使其具有用于集成和验收测试的共享文件夹,但您也可以使用原始文件夹结构。此外,您在评论中提到您希望将所有三个(包括未提及的集成测试)分开,这是可能的,虽然有点骇人听闻。

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

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 1970-01-01
    • 2010-10-28
    • 2019-05-05
    • 2017-01-09
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多