如何将Maven与TestNG集成

2024-03-30 10:20
文章标签 maven 集成 testng

本文主要是介绍如何将Maven与TestNG集成,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们已经讨论了如何在maven中执行单元测试用例,但那些是JUnit测试用例,而不是TestNG。当maven使用“mvn test”命令进入测试阶段时,这些用例被执行。

本文将介绍如何将Maven与TestNG集成,并在maven进入测试阶段时执行TestNG测试。

默认情况下,Maven执行src/test/java目录中的单元测试用例,我们将只遵循这个规范,并在src/test/java目录中生成用例。

我们将通过一系列步骤将Maven与TestNG集成在一起-

  • 创建maven项目
  • 添加maven依赖项和插件
  • 添加TestNG测试
  • 创建testng.xml文件
  • 最后, 执行TestNG测试 
创建maven项目

我们可以通过在命令行工具上运行下面的命令来轻松地创建一个项目。

mvn archetype:generate -DgroupId=org.website.codekru -DartifactId=DemoProject -DpackageName=org.website.codekru -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

请阅读这篇文章,了解更多关于如何创建maven项目的信息。

接下来,将在我们运行命令的目录中创建一个maven项目,它将具有maven创建的默认项目结构。

Project structure

添加maven依赖项和插件

我们将在pom.xml文件中添加TestNG maven依赖项和maven surefire插件。

TestNG maven dependency

<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.6.1</version>
</dependency>

Maven surefire plugin dependency

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M7</version></plugin></plugins></build>

整个pom.xml现在将是这样的-

<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.website.codekru</groupId><artifactId>DemoProject</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>DemoProject</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.testng/testng --><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.6.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M7</version></plugin></plugins></build>
</project>
添加TestNG测试

我们现在将在AppTest类中添加TestNG测试。一个测试用例是一个用TestNG的@Test注释标记的方法。

package org.website.codekru;import org.testng.annotations.Test;public class AppTest {@Testpublic void test1() {System.out.println("Executing test1");}@Testpublic void test2() {System.out.println("Executing test2");}
}

我们在AppTest类中添加了两个TestNG测试。现在我们必须处理这些案件。

创建testng.xml文件

在项目的根目录下创建一个testng.xml文件。xml文件有助于在TestNG中执行测试用例。

下面是testng.xml文件中的内容。

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" ><suite name="codekru"><test name="codekruTest"><classes><class name="org.website.codekru.AppTest" /></classes></test>
</suite>

上面的XML文件将执行AppTest类中的所有用例。请阅读本文以了解如何创建testng.xml文件。

这是我们更新的项目结构。

Updated project structure

执行测试用例

这些测试用例应该在maven执行其测试阶段时执行。

为此,我们必须对XML文件进行一些更改。如果你还记得的话,我们在前面添加了maven surefire插件,现在我们必须为插件添加一些配置。

早些时候

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M7</version></plugin></plugins></build>

现在

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M7</version><configuration><suiteXmlFiles><suiteXmlFile>testng.xml</suiteXmlFile></suiteXmlFiles></configuration></plugin></plugins>
</build>

新添加的配置将在maven执行测试阶段时运行上述testng.xml文件。

所以,现在我们更新的pom.xml将是

<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.website.codekru</groupId><artifactId>DemoProject</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>DemoProject</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.testng/testng --><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.6.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M7</version><configuration><suiteXmlFiles><suiteXmlFile>testng.xml</suiteXmlFile></suiteXmlFiles></configuration></plugin></plugins></build>
</project>

现在,从命令行运行“mvn test”命令,现在也将执行TestNG测试。

Result of mvn test command

但是如果你得到下面的错误。

Error in compilation

 请在pom.xml文件中添加以下属性。

<properties><maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target>
</properties>

所以,现在更新的pom.xml文件将是-

<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.website.codekru</groupId><artifactId>DemoProject</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>DemoProject</name><url>http://maven.apache.org</url><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.testng/testng --><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.6.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M7</version><configuration><suiteXmlFiles><suiteXmlFile>testng.xml</suiteXmlFile></suiteXmlFiles></configuration></plugin></plugins></build>
</project>

从命令行再次运行“mvn test”命令,它将执行TestNG测试。

Result after mvn test command

最后,执行了TestNG测试;这就是我们如何通过使用maven surefire插件来集成maven和TestNG

这篇关于如何将Maven与TestNG集成的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/861230

相关文章

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

IDEA Maven提示:未解析的依赖项的问题及解决

《IDEAMaven提示:未解析的依赖项的问题及解决》:本文主要介绍IDEAMaven提示:未解析的依赖项的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录IDEA Maven提示:未解析的依编程赖项例如总结IDEA Maven提示:未解析的依赖项例如

SpringBoot集成LiteFlow工作流引擎的完整指南

《SpringBoot集成LiteFlow工作流引擎的完整指南》LiteFlow作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择,本文将详细讲解Sp... 目录一、LiteFlow核心优势二、SpringBoot集成实战三、高级特性应用1. 异步并行执行2

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho

Maven中的profiles使用及说明

《Maven中的profiles使用及说明》:本文主要介绍Maven中的profiles使用及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录主要用途定义 Profiles示例:多环境配置激活 Profiles示例:资源过滤示例:依赖管理总结Maven 中的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

maven私服配置全过程

《maven私服配置全过程》:本文主要介绍maven私服配置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录使用Nexus作为 公司maven私服maven 私服setttings配置maven项目 pom配置测试效果总结使用Nexus作为 公司maven私