TestNG 如何在Maven中排除TestNG组

TestNG 如何在Maven中排除TestNG组

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

Maven提供了灵活性,可以在运行时包含或排除测试组。用户可以使用surefire插件在Maven中在运行时排除一个或多个测试组。

在这个教程中,我们将说明如何在运行时通过surefire排除一个测试组。

步骤

  • 步骤1:创建一个TestNG类 – NewTestngClass

  • 步骤2:在类中编写两个带有组名的@Test方法

  • 步骤3:现在创建testNG.xml,其中不要提及组的详细信息

  • 步骤4:在pom.xml中的suiteXMLFiles中不添加应该排除的组名,示例如程序节所示

  • 步骤5:现在,使用命令行运行它

示例

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

src/ NewTestngClass.java

import org.testng.annotations.Test;

public class NewTestngClass {

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

pom.xml

这是一个使用Maven配置依赖关系、插件和运行TestNG测试用例的文件。

在只需要执行少量测试用例而不是整个套件时非常方便。

如果用户想要在pom.xml中排除某些分组,则需要在</suiteXmlFiles>标签后添加<excludedGroups>group1,group2</excludedGroups>。在这种情况下,用户不需要通过命令行传递参数。

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

运行单个类的命令

请注意,如果需要排除多个组,请使用逗号将所有组名称分隔开,例如 −DexcludedGroups=group1,group3

mvn test −DexcludedGroups=group1

输出

[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 0 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] skip non existing resourceDirectory C:\Users\anandas\IdeaProjects\TestNGProjectct\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct ---
[INFO] No tests to run.
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
...
... TestNG 7.3.0 by Cédric Beust (cedric@beust.com)
...
in test case 2 of NewTestngClass

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.461 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------
[INFO] Total time:  2.838 s
[INFO] Finished at: 2022-02-08T16:07:17+05:30
[INFO] --------------------------------------------------------

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程