【问题标题】:Specifying multiple goals in Maven and running them separately在 Maven 中指定多个目标并分别运行它们
【发布时间】:2017-07-05 04:14:01
【问题描述】:

我使用 Cucumber、Selenium 和 Maven 创建了一个测试框架。我正在使用cucumber-jvm-parallel 插件并行运行我的测试。下面是我的pom.xml 的样子:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.tests</groupId>
<artifactId>test-project</artifactId>
<version>1.0</version>

<name>Test Project</name>
<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <cucumber.version>1.2.3</cucumber.version>
    <extentreports.version>2.41.0</extentreports.version>
    <selenium.version>2.53.1</selenium.version>
    <cucumber.jvm.parallel.version>2.1.0</cucumber.jvm.parallel.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.github.temyers</groupId>
        <artifactId>cucumber-jvm-parallel-plugin</artifactId>
        <version>${cucumber.jvm.parallel.version}</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>${cucumber.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>1.7.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${cucumber.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sitture</groupId>
        <artifactId>cucumber-jvm-extentreport</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.directory.studio</groupId>
        <artifactId>org.apache.commons.codec</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>com.vimalselvam</groupId>
        <artifactId>cucumber-extentsreport</artifactId>
        <version>1.1.1</version>
    </dependency>

</dependencies>
<profiles>
    <profile>
        <id>win</id>
        <activation>
            <os>
                <family>windows</family>
            </os>
        </activation>
        <properties>
            <webdriver.chrome.path>${basedir}${file.separator}resources${file.separator}chromedriver.exe</webdriver.chrome.path>
        </properties>
    </profile>
    <profile>
        <id>linux</id>
        <activation>
            <os>
                <family>!windows</family>
            </os>
        </activation>
        <properties>
            <webdriver.chrome.path>resources${file.separator}chromedriver</webdriver.chrome.path>
        </properties>
    </profile>
</profiles>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.github.temyers</groupId>
            <artifactId>cucumber-jvm-parallel-plugin</artifactId>
            <version>${cucumber.jvm.parallel.version}</version>
            <executions>
                <execution>
                    <id>generateRunners</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>generateRunners</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/generated-test-sources/cucumber</outputDirectory>
                        <featuresDirectory>src/test/resources/features</featuresDirectory>
                        <cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>
                        <format>json,html,rerun</format>
                        <strict>true</strict>
                        <monochrome>true</monochrome>
                        <useTestNG>false</useTestNG>
                        <namingPattern>Parallel{c}IT</namingPattern>
                        <namingScheme>simple</namingScheme>
                        <parallelScheme>FEATURE</parallelScheme>
                        <glue>com.cucumber.stepdefinitions</glue>
                        <tags>"~@ignore"</tags>
                        <filterFeaturesByTags>true</filterFeaturesByTags>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>resources</additionalClasspathElement>
                </additionalClasspathElements>
                <!-- Increase Fork Count to increase parallel execution count.
                Currently it is set to 2 which means 2 runners will run in parallel-->
                <forkCount>2</forkCount>
                <reuseForks>true</reuseForks>
                <includes>
                    <include>**/Parallel*IT.java</include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>default</id>
                    <goals>
                        <goal>perform</goal>
                    </goals>
                    <configuration>
                        <pomFileName>${basedir}${file.separator}pom.xml</pomFileName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我用

mvn clean install

运行我的套件。运行完成后,我在 Jenkins 中配置了一个下游作业,它会生成一些失败的案例文件。我为这些创建单独的跑步者。现在我希望重新运行失败的测试用例,并创建了名为failedScenario1.java、failedScenario2.java 等的文件。我现在想运行这些。我假设我需要在上面的 POM 中定义类似于 org.apache.maven.plugins 的内容并触发运行。有人可以让我知道如何将其包含在同一个 POM 中。另外我怎样才能为failedScenario*.java触发这个运行。

【问题讨论】:

  • 这并不能解决您的问题,但是否有意使用 maven-compiler-plugin 的 3.1 版?这已经快 5 岁了。 maven-release-plugin:2.2.1 更老了。
  • 我说对了吗:您正在考虑动态创建新的 Maven 项目(新 POM,pom.xmls),使用 failedScenario*.java 作为测试源,然后构建这些项目,包括 @ 987654335@相?
  • 没有。实际上,我在考虑是否可以在同一个 pom 中指定一个配置文件或目标,它只会运行 failedscenarios*.java

标签: java maven selenium jenkins cucumber


【解决方案1】:

恐怕这在同一个 POM 中不可能完全按照您的喜好。

你需要类似下面的东西:

  <profiles>
    <profile>
      <id>suite</id>
      <activation>
        <property>
          <name>re-test</name>
          <value>!true</value>
        </property>
      <activation>

      ... your suite's declarations ...

    </profile>

    <profile>
      <id>failed-scenario</id>
      <activation>
        <property>
          <name>re-test</name>
          <value>true</value>
        </property>
      <activation>
      <build>
        <testSourceDirectory>${project.build.directory}/generated-test-sources/cucumber</testSourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
              <executions>
                <execution>
                  <id>failed-scenario-test-compile</id>
                  <goals>
                    <goal>testCompile</goal>
                  </goals>
                </execution>
              </executions>
            ...
         <plugin>
           <artifactId>maven-surefire-plugin</artifactId>
             ...  

但是,请参阅Introduction to Build Profiles, Profiles in POMs:

在 POM 中指定的配置文件可以修改以下 POM 元素:

  • ...

  • &lt;build&gt; 元素的子集,包括:

    •&lt;defaultGoal&gt;

    •&lt;resources&gt;

    •&lt;testResources&gt;

    •&lt;finalName&gt;

所以,&lt;testSourceDirectory&gt; 不见了。

如果您要将generate-test-sources 设置为默认的&lt;testSourceDirectory&gt;${project.basedir}/src/test/java,您可以在re-test 配置文件中使用maven-surefire-plugin 的&lt;excludes&gt;/&lt;includes&gt;。缺点是所有failedScenarioX 也会在您下次构建套件时运行,除非您之前删除它们。

【讨论】:

  • 我参考了您的答案并提出了类似的解决方案。我正在创建一个名为 Failed-Cases 的配置文件来运行失败的测试并使用 clean install -PFailed-Cases 来运行失败的场景。运行完成后,我正在删除运行器文件。然而,这运作良好,它似乎更像是一个热修复而不是永久解决方案。还有其他实现这一点的想法吗?
  • @BountyHunter Re “更像是一个热修复程序”:无论如何,从 Maven 的角度来看,这不是一个常见的场景。通常是一个项目,一个测试(套件),一组测试结果,反映了整个项目的所有测试。我唯一的另一个想法是将failedScenarioXs 转移到一个单独的项目中,原始项目是它的依赖项之一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 1970-01-01
相关资源
最近更新 更多