无法通过maven surefire插件

时间:2017-12-05 21:32:42

标签: maven testing maven-plugin maven-surefire-plugin

我有一个非常简单的测试项目设置,我有多个suiteXmlFiles。 pom看起来如下:

<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>testautomation</groupId>
<artifactId>testautomation</artifactId>
<version>0.0.1-SNAPSHOT</version>

 <properties>
    <log4j.version>2.8.2</log4j.version>
    <selenium.version>2.53.0</selenium.version>
    <testng.version>6.10</testng.version>       
    <relevantcodes.version>2.41.1</relevantcodes.version>
    <aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version>
    <aws-java-sdk-dynamodb.version>1.11.139</aws-java-sdk-dynamodb.version>
    <rest-assured.verison>2.9.1-SNAPSHOT</rest-assured.verison>
    <json-path.version>2.9.0</json-path.version>
    <json.version>20160810</json.version>
    <xml-path.version>2.9.0</xml-path.version>
    <json-schema-validator.verison>2.9.0</json-schema-validator.verison>
    <poi.verison>3.15</poi.verison>
    <poi-ooxml.verison>3.15</poi-ooxml.verison>
    <poi-ooxml-schemas.version>3.15</poi-ooxml-schemas.version>
    <poi-scratchpad.version>3.2-FINAL</poi-scratchpad.version>
    <jackson-databind.version>2.8.5</jackson-databind.version>  
    <maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version>
    <opencsv>3.10</opencsv>
    <java.version>1.8</java.version>
    <!-- <maven.jar.plugin.version>3.0.2</maven.jar.plugin.version> -->
    <maven.jar.plugin.version>2.6</maven.jar.plugin.version>
    <maven-install-plugin.version>2.5.2</maven-install-plugin.version>

     <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
     <!--<maven-surefire-plugin.version>2.12</maven-surefire-plugin.version> -->
    <skipTests>false</skipTests>
     <sometest>src/test/resources/testngxmls/sometest/test1.xml</sometest>
    <smoketest>src/test/resources/testngxmls/smoketest/test2.xml</smoketest>


</properties>


<build>
    <!-- <sourceDirectory>src</sourceDirectory> -->
    <plugins>

     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <inherited>true</inherited>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>               

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>${maven.jar.plugin.version}</version>
            <inherited>true</inherited>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>${maven-install-plugin.version}</version>
            <executions>
                <execution>
                    <id>default-install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>install</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
             <configuration>
                <skipTests>${skipTests}</skipTests>
                <suiteXmlFiles>
                    <xmlFile>${sometest}</xmlFile>
                    <xmlFile>${smoketest}</xmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>${testng.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <dependency>
        <groupId>com.relevantcodes</groupId>
        <artifactId>extentreports</artifactId>
        <version>${relevantcodes.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>${poi.verison}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>${poi-ooxml.verison}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>${poi-ooxml-schemas.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>${poi-scratchpad.version}</version>
    </dependency>

    <!--adding only required jars of AWS -->

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>${aws-lambda-java-core.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-dynamodb</artifactId>
        <version>${aws-java-sdk-dynamodb.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>${rest-assured.verison}</version>
    </dependency>

    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>json-path</artifactId>
        <version>${json-path.version}</version>
    </dependency>

    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>xml-path</artifactId>
        <version>${xml-path.version}</version>
    </dependency>

    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>${json-schema-validator.verison}</version>
        <!-- <scope>test</scope> -->
        <scope>compile</scope>
    </dependency>

    <dependency>
         <groupId>com.opencsv</groupId>
         <artifactId>opencsv</artifactId>
         <version>${opencsv}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-databind.version}</version>
    </dependency>

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>${json.version}</version>
    </dependency>

    <dependency>

        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>3.0</version>
    </dependency>

     <!-- <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-text</artifactId>
        <version>1.1</version>
    </dependency> -->
</dependencies>

<repositories>
    <repository>
        <id>sonatype</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        <snapshots />
    </repository>
</repositories>

使用这个pom.xml。我一直在尝试使用mvn命令行独立运行选定的套件,但它会继续执行所有的suiteXmlFiles。我使用的命令如下:

当我运行此命令$ mvn -e clean test -Dsurefire.xmlFile=sometest时,它运行sometest套件,然后还在smoketest中运行所有测试。使用下面列出的所有命令进行类似的观察。

$ mvn -e clean test -Dsurefire.xmlFiles=sometest

我甚至尝试将xmlFile更改为suiteXmlFile和文件标签并运行相应的命令:

$ mvn -e clean test -Dsurefire.suiteXmlFile=sometest $ mvn -e clean test -Dsurefire.suiteXmlFiles=sometest

$ mvn -e clean test -Dsurefire.suiteXmlFile=sometest $ mvn -e clean test -Dsurefire.suiteXmlFiles=sometest

这些测试是否可以独立运行,坚持使用suiteXmlFiles。这看起来像一个简单的功能,令人费解的是为什么要让它运行起来如此困难。

1 个答案:

答案 0 :(得分:1)

似乎与testng-6.13.1和maven-surefire-plugin 2.20一起正常工作。检查工件版本,或者必须将正确的文件路径传递给Dsurefire.suiteXmlFile选项。

MVN命令:

 mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testXml1.xml

Test Suite 1的输出:

[INFO] Running TestSuite
This is TestNG-Test-Suite-First Example
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.345 s - in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

MVN命令:

 mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testXml2.xml

Test Suite 2的输出:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
@BeforeTest
This is TestNG-Test-Suite-Second Example
@AfterTest

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.399 s - in TestSuite
[INFO]

项目设置

<强>的pom.xml

 <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.13.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>src/test/resources/testXmlAll.xml</suiteXmlFile>
                            <suiteXmlFile>src/test/resources/testXml1.xml</suiteXmlFile>
                            <suiteXmlFile>src/test/resources/testXml2.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

<强> testXml1.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test1">
    <test name="test1">
        <classes>
            <class name="TestNgSuiteFirstTest"/>
        </classes>
    </test>
</suite>

<强> testXml2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test2">
    <test name="test2">
        <classes>
            <class name="TestNgSuiteSecondTest"/>
        </classes>
    </test>
</suite>

将套件文件作为参数传递

这只是将套装文件作为参数

传递的解决方法

配置属性变量以指向每个套件文件。

<properties>
   <test1SuiteFile>${project.basedir}/src/test/resources/testXml1.xml</test1SuiteFile>
        <test2SuiteFile>${project.basedir}/src/test/resources/testXml2.xml</test2SuiteFile>
        <!-- Default suites -->
        <testAllSuiteFile>${project.basedir}/src/test/resources/testXmlAll.xml</testAllSuiteFile>
        <suiteFile>${testAllSuiteFile}</suiteFile>
    </properties>

更新插件

 <configuration>
     <suiteXmlFiles>
          <suiteXmlFile>${suiteFile}</suiteXmlFile>
      </suiteXmlFiles>
  </configuration>

在命令行中将文件名作为参数传递

mvn clean test
mvn test -DsuiteFile=${test2SuiteFile}
mvn test -DsuiteFile=${test1SuiteFile},${test2SuiteFile}

否则按如下方式定义配置并传递单个文件:

mvn test -DsuiteFile=testXml1.xml

   <configuration> 
             <suiteXmlFile>${project.basedir}/src/test/resources/${suiteFile}
                            </suiteXmlFile>
  </suiteXmlFiles>
</configuration>