testng+maven+java+idea

本文介绍了如何在IntelliJ IDEA中创建Maven项目并集成TestNG进行自动化测试。首先,通过设置Maven项目的GroupID和ArtifactID来创建项目。接着,在pom.xml中添加TestNG的依赖。然后,创建测试类并配置testng.xml文件,包括插件配置。在pom文件中,关联TestNG配置以指定suiteXmlFile路径。运行testng.xml文件,测试报告将自动生成于test-output文件夹中,打开index.html可查看详细测试报告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 在ide中创建maven项目
创建过程
点击next

  1. GroudId:一般为公司的域名,如www.baidu.com
  2. ArtifiactId:一般为项目的名称

点击finish

2. 在pom.xml中添加testng的有关依赖,如下:

public class TestDemo {
    @Test
    public void testcase2(){
        Assert.assertTrue(true);
        System.out.println("testcase1");
    }
}

3. 创建测试类

public class TestDemo {
    @Test
    public void testcase2(){
        Assert.assertTrue(true);
        System.out.println("testcase1");
    }
}

4.在项目目录中添加testng.xml,最简单的格式如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="TestNG1">
        <classes>
            <class name="TestDemo"></class>
        </classes>
    </test>
</suite>

其中可以添加插件

这里写图片描述

安装完成后需要重启idea

5. maven与testng进行在pom文件中进行关联配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.15</version>
            <configuration>
                <!--<testFailureIgnore>true</testFailureIgnore>-->
                <forkMode>never</forkMode>
                <argLine>-Dfile.encoding=UTF-8</argLine>
                <suiteXmlFiles>
                    <suiteXmlFile>res/testNG.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

suiteXmlFile的路径为testNG.xml的路径

6. 运行testng.xml文件即可
直接运行testng.xml文件后,生成报告如下
这里写图片描述

7. 设置后重新运行testng.xml会创建test-output文件夹
这里写图片描述
这里写图片描述
8. 打开test-output文件夹中的index.html文件
生成如下的测试报告
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值