我有一个非常简单的测试项目设置,其中有多个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独立运行这些测试。这看起来像一个简单的功能,令人困惑的是为什么很难启动和运行它。

最佳答案

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

MVN命令:

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


测试套件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


测试套件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>

关于maven - 无法通过Maven surefire插件运行选定的suiteXmlFile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47663004/

10-10 05:08