TestNG 如何在使用maven时不使用testng.xml运行TestNG类

TestNG 如何在使用maven时不使用testng.xml运行TestNG类

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

Maven提供了使用surefire插件运行的灵活性。它允许用户运行testng.xml,也可以直接运行一个TestNG类而不使用testng.xml。实现这一功能需要满足一些前提条件:

  • 所有的testng类都应该创建在src/test/java下。如果类没有创建在这些目录下,用户应该在pom.xml文件中传递testng.xml。

  • 默认情况下,Maven surefire插件识别以下类:

    • **/Test*.java – 包括其所有子目录和所有以”Test”开头的Java文件名。

    • **/*Test.java– 包括其所有子目录和所有以”Test”结尾的Java文件名。

    • **/*Tests.java– 包括其所有子目录和所有以”Tests”结尾的Java文件名。

    • **/*TestCase.java– 包括其所有子目录和所有以”TestCase”结尾的Java文件名。

本文将介绍如何在使用maven时不使用testng.xml运行TestNG类。

步骤

  • 第1步:在src/test/java下创建一个TestNG类 – NewTes

  • 第2步:在类中编写2个@Test方法。

  • 第3步:确保pom.xml中的suiteXMLFiles标签被删除。

  • 第4步:现在,在命令行中使用mvn test运行它。它将运行项目中满足上述条件的所有类。

示例

以下代码展示了如何只运行大测试套件中的一个测试方法:

src/test/java/NewTest.java

import org.testng.annotations.Test;

public class NewTest {

    @Test()
    public void testCase1() {
        System.out.println("in test case 1 of NewTest");
    }
   @Test(groups = { "group2", "group3" })
    public void testCase2() {
        System.out.println("in test case 2 of NewTest");

    }


}

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>
            </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>

运行特定测试套件的命令

mvn test

输出

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.sample:TestNGProjectct >---------------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGProjectct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGProjectct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running newTest
in test case 1 of NewTest 
in test case 2 of NewTest 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.73 s - in newTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.916 s
[INFO] Finished at: 2022-02-10T08:32:07+05:30
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程

TestNG 精选笔记