简短说明
我的eclipse Maven项目具有运行所有测试的默认TestNG套件(在testng.xml
文件中)和定制的TestNG套件(仅运行指定的测试)。在eclipse IDE中,我可以运行任何TestNG套件,但是从Windows 10命令行(mvn test -Dsurefire.suiteXmlFiles=/path/to/customized_TestNG_file
),定制的TestNG套件将被忽略,仅默认的TestNG套件(在testng.xml
文件中)被执行。
详细信息
TestNGProj
是一个eclipse Maven项目,其中包含两个对接的Maven模块TestNGMod1
和TestNGMod2
。 TestNGProj
的Project Explorer视图如下:
TestNGMod1
的Project Explorer视图如下:
pom
文件TestNGProj
如下:
<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>
pom
的{{1}}文件如下:
TestNGMod1
<?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>
的源代码如下:
TestsForPkg1
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 *********************");
}
}
的源代码如下:
TestsForPkg2
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 *********************");
}
}
中指定的默认测试套件如下:
testng.xml
专业测试套件<?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 -->
如下:
testng-Pkg1.xml
专业测试套件<?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 -->
如下:
testng-Pkg2.xml
在eclipse中,我可以运行任何测试套件而没有任何问题。
当我尝试从Windows命令行运行测试套件时,会出现问题。我已经使用了项目目录中的各种命令字符串,但是无论我做什么,都只会执行默认的TestNG套件(在<?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 -->
文件中)。
我尝试了以下操作:
testng.xml
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
可以看出,在所有情况下,都是执行的默认测试套件(包含所有测试)。如前所述,在eclipse中并非如此,可以调用所有所需的测试套件,而不会出现任何问题。