如何在 Maven 中从 pom.xml 调用 testng.xml 文件?

testngrest assureddynamic programming更新于 2025/5/6 4:07:17

Maven 是一个项目管理和理解工具,它提供了完整的构建生命周期框架。由于 Maven 使用标准目录布局和默认构建生命周期,因此用户几乎可以立即自动化项目的构建基础架构。

在存在多个环境的情况下,Maven 可以在很短的时间内按照标准设置工作方式。由于大多数项目设置都很简单且可重复使用,因此 Maven 在创建报告、检查、构建和测试自动化设置时让生活变得轻松。

Maven 为开发人员提供了管理以下内容的方法 -

  • Builds

  • Documentation

  • Reporting

  • Dependencies

  • SCMs

  • Releases

  • Distribution

  • Mailing list

总而言之,Maven 简化并标准化了项目构建过程。它无缝处理编译、分发、文档、团队协作和其他任务。 Maven 提高了可重用性并处理了大多数与构建相关的任务。

TestNG 是一个测试框架,可以使用 Maven 作为构建工具。它有助于在 pom.xml 中的一个位置维护依赖项及其版本

用户可以从 testng.xml 或 pom.xml 运行测试。要从 pom.xml 运行测试,用户需要提及 testng.xml 的路径并需要 maven-surefire-plugin 来执行。

在本文中,我们将了解在 pom.xml 中提及 testng.xml 详细信息的位置和格式

解决此问题的方法/算法

  • 步骤 1:创建 TestNG 类 - NewTestngClass

  • 步骤 2:在所有类中编写 @Test 方法。

  • 步骤 3:现在创建如下所示的 testNG.xml。

  • 步骤 4:在 pom.xml 中添加 testng.xml 的路径,如下所示。

<configuration>
       <suiteXmlFiles>
             <suiteXmlFile>Path of testng.xml</suiteXmlFile>
        </suiteXmlFiles>
  </configuration>

在配置部分,提到了 testng 的路径。有两件事要记住,第一,此配置应与 maven-surefire-plugin 一起保存,第二,永远不要忘记添加 testng 的依赖项。

  • 步骤 5:现在,在 IDE 中运行 pom.xml 或使用命令行编译并运行它。

示例

以下代码显示如何从大型套件中仅运行 1 个测试方法:

src/ NewTestngClass.java

import org.testng.annotations.Test;

public class NewTestngClass {

    @Test
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass");
    }
    
}  

testng.xml

这是一个用于组织和运行 TestNG 测试用例的配置文件。

当需要执行有限的测试而不是全套测试时,它非常方便。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1" parallel = "none">
    <test name = "test1" preserve-order = "true">
        <classes>
		<class name="NewTestngClass"/>
	   </classes>
    </test>
</suite>

pom.xml

这是一个 maven 配置文件,用于组织依赖项、插件和运行 TestNG 测试用例。

当需要执行有限的测试而不是全套测试时,它非常方便。

<?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.sample</groupId>
    <artifactId>TestNGProjectct</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <suiteXmlFiles>
                            <suiteXmlFile>src/main/java/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>          
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
    </dependencies>
</project>

输出

4.0.0

    com.sample
    TestNGProjectct
    1.0-SNAPSHOT
    
    org.apache.maven.plugins
    maven-surefire-plugin
    3.0.0-M5
                
    src/main/java/testng.xml


相关文章