如何将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

相关文章

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service

springboot2.1.3 hystrix集成及hystrix-dashboard监控详解

《springboot2.1.3hystrix集成及hystrix-dashboard监控详解》Hystrix是Netflix开源的微服务容错工具,通过线程池隔离和熔断机制防止服务崩溃,支持降级、监... 目录Hystrix是Netflix开源技术www.chinasem.cn栈中的又一员猛将Hystrix熔

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

SpringBoot集成P6Spy的实现示例

《SpringBoot集成P6Spy的实现示例》本文主要介绍了SpringBoot集成P6Spy的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录本节目标P6Spy简介抛出问题集成P6Spy1. SpringBoot三板斧之加入依赖2. 修改

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

SpringBoot集成Shiro+JWT(Hutool)完整代码示例

《SpringBoot集成Shiro+JWT(Hutool)完整代码示例》ApacheShiro是一个强大且易用的Java安全框架,提供了认证、授权、加密和会话管理功能,在现代应用开发中,Shiro因... 目录一、背景介绍1.1 为什么使用Shiro?1.2 为什么需要双Token?二、技术栈组成三、环境

Java 与 LibreOffice 集成开发指南(环境搭建及代码示例)

《Java与LibreOffice集成开发指南(环境搭建及代码示例)》本文介绍Java与LibreOffice的集成方法,涵盖环境配置、API调用、文档转换、UNO桥接及REST接口等技术,提供... 目录1. 引言2. 环境搭建2.1 安装 LibreOffice2.2 配置 Java 开发环境2.3 配

SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南

《SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南》本文将基于开源项目springboot-easyexcel-batch进行解析与扩展,手把手教大家如何在SpringBo... 目录项目结构概览核心依赖百万级导出实战场景核心代码效果百万级导入实战场景监听器和Service(核心

idea Maven Springboot多模块项目打包时90%的问题及解决方案

《ideaMavenSpringboot多模块项目打包时90%的问题及解决方案》:本文主要介绍ideaMavenSpringboot多模块项目打包时90%的问题及解决方案,具有很好的参考价值,... 目录1. 前言2. 问题3. 解决办法4. jar 包冲突总结1. 前言之所以写这篇文章是因为在使用Mav