Non-default testng file ignored during command line execution (-Dsurefire)

我是研究僧i 提交于 2020-01-11 13:09:10

问题


Short Description

My Eclipse Maven project has a default TestNG suite (in the testng.xml file), which runs all the tests, and customized TestNG suites, which runs only specified tests. From within the Eclipse IDE, I am able to run any TestNG suite but from the Windows 10 command line (mvn test -Dsurefire.suiteXmlFiles=/path/to/customized_TestNG_file) the customized TestNG suites are ignored and only the default TestNG suite (in the testng.xml file) is executed.


Details

TestNGProj is an Eclipse Maven Project that contains two subtending Maven Modules TestNGMod1 and TestNGMod2. The Project Explorer view of TestNGProj is as follows:

The Project Explorer view of TestNGMod1 is as follows:

The pom file TestNGProj is as follows:

<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>org.newbie</groupId>
    <artifactId>TestNGProj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>TestNGProj</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

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

    </dependencies>
    <modules>
        <module>TestNGMod1</module>
        <module>TestNGMod2</module>
    </modules>
</project>

The pom file for TestNGMod1 is as follows:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.newbie</groupId>
    <artifactId>TestNGProj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>org.newbie</groupId>
  <artifactId>TestNGMod1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>TestNGMod1</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>  

The source code for TestsForPkg1 is as follows:

package Pkg01;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

public class TestsForPkg1 {
    @BeforeClass
    public void setup() {
        System.out.println("\n********************* Pkg1::setup *********************");
     }

    @Test
    public void method1Pkg1Test() {
        System.out.println("\n********************* method1Pkg1Test *********************");
     }

    @Test
    public void method2Pkg1Test() {
        System.out.println("\n********************* method2Pkg1Test *********************");
     }
}

The source code for TestsForPkg2 is as follows:

package Pkg02;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

public class TestsForPkg2 {
    @BeforeClass
    public void setup() {
        System.out.println("\n********************* Pkg2::setup *********************");
     }

    @Test
    public void method1Pkg2Test() {
        System.out.println("\n********************* method1Pkg2Test *********************");
     }

    @Test
    public void method2Pkg2Test() {
        System.out.println("\n********************* method2Pkg2Test *********************");
     }
}

The default test suite, as specified in testng.xml, is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="Pkg01.TestsForPkg1"/>
      <class name="Pkg02.TestsForPkg2"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Specialized test suite testng-Pkg1.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="Pkg01.TestsForPkg1"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->  

Specialized test suite testng-Pkg2.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="Pkg02.TestsForPkg2"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

From within Eclipse I can run any test suite without any issues.

The problem arises when I try to run the test suites from the Windows command line. I have used various command strings from the project directory but no matter what I do, it is only the default TestNG suite (in the testng.xml file) that gets executed.

I tried the following:

mvn test -Dsurefire.suiteXmlFiles=testng-Pkg1.xml

mvn test -Dsurefire.suiteXmlFiles="C:\Workspace Stackoverflow\TestNGProj\TestNGMod1\testng-Pkg1.xml"

mvn test -Dsurefire.suiteXmlFiles=testng-Pkg2.xml

mvn test -Dsurefire.suiteXmlFiles="C:\Workspace Stackoverflow\TestNGProj\TestNGMod1\testng-Pkg2.xml"

In each case I got an output similar to this:


As can be seen, in all instances it is the default test suite (which has all the tests) that gets executed. As mentioned earlier, from within Eclipse this is not the case and all the desired test suites can be invoked, without any issues.

来源:https://stackoverflow.com/questions/58868103/non-default-testng-file-ignored-during-command-line-execution-dsurefire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!