Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation。
本章就是非常简单的讲一下TestNG的使用入门。为下一章二次开发做铺垫。
原文地址:
创建maven项目
然后在pom文件中,添加依赖。
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>6.14.3</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>6.14.3</version>
- <scope>compile</scope>
- </dependency>
常用注解,这里也是管理case的运行顺序等,
@BeforeSuite / @AfterSuite
@BeforeTest / @AfterTest
@BeforeClass / @AfterClass,在类运行之前/后运行
@BeforeMethod / @AfterMethod,在测试方法之前/后运行
- package com.qzcsbj;
-
- import org.testng.annotations.*;
- import org.testng.annotations.Test;
-
- /**
- * @描述 : <...>
- * @博客 : www.cnblogs.com/uncleyong
- * @微信 : ren168632201
- */
- public class TestAnnotation {
- @Test
- public void test(){
- System.out.println("TestAnnotation.test");
- System.out.println("线程ID:" + Thread.currentThread().getId());
- }
-
- @Test
- public void test2(){
- System.out.println("TestAnnotation.test2");
- }
-
- @BeforeMethod
- public void beforeMethodTest(){
- System.out.println("TestAnnotation.beforeMethodTest");
- }
-
- @AfterMethod
- public void afterMethodTest(){
- System.out.println("TestAnnotation.afterMethodTest");
- }
-
- @BeforeClass
- public void beforeClassTest(){
- System.out.println("TestAnnotation.beforeClassTest");
- }
-
- @AfterClass
- public void afterClassTest(){
- System.out.println("TestAnnotation.afterClassTest");
- }
-
- @BeforeSuite
- public void beforeSuiteTest(){
- System.out.println("TestAnnotation.beforeSuiteTest");
- }
-
- @AfterSuite
- public void afterSuiteTest(){
- System.out.println("TestAnnotation.afterSuiteTest");
- }
- }
测试过程中,问题还没解决,可以先忽略,也就是不执行此方法。
- import org.testng.annotations.Test;
-
- /**
- * @博客 : www.cnblogs.com/uncleyong
- * @微信 : ren168632201
- * @描述 : <...>
- */
- public class TestIgnore {
- @Test
- public void testa(){
- System.out.println("TestIgnore.testa");
- }
-
- @Test(enabled = true)
- public void testb(){
- System.out.println("TestIgnore.testb");
- }
-
- @Test(enabled = false)
- public void testc(){
- System.out.println("TestIgnore.testc");
- }
- }
testNG使用XML文件来管理case的执行。比如要运行哪些case。
这个XML文件可以手动创建,也可以使用插件来生成。我们这里使用插件来生成xml文件。
Preferences-->Plugins

安装完Create TestNG XML插件后,再去生成testng.xml文件。

Create TestNG XML
然后重新一下项目,就会看到生成的testng.xml文件了。
testng.xml就是我们管理测试case运行的套件,文件名字可以自己命名。
xml文件讲解:
- "1.0" encoding="UTF-8"?>
- "http://testng.org/testng-1.0.dtd">
"All Test Suite"> -
-
"test"> -
- <class name="com.qzcsbj.TestAnnotation"/>
-
-
-
"test2"> -
- <class name="com.qzcsbj.TestAnnotationB">
-
-
"testb"/> -
-
-
-
执行测试,运行xml文件就可以了。
在xml文件里,右键,选择Run 这个xml文件。
其他的一些细节,看原文,讲的很细节。TestNG基本使用 - 全栈测试笔记 - 博客园
Java自动化测试之TestNG生成测试报告_Leoon123的博客-CSDN博客_testng生成测试报告
Java接口自动化测试(六) — ZTestReport测试报告
其他博主推荐使用ZTestReport报告,既美观,且展示数据简洁。
ZTestReport最重要的两个文件TestReport,template。
配置POM.xml文件,添加依赖:
- <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.0</version>
- </dependency>
下载地址:
GitHub - zhangfei19841004/ztest: 自动化测试报告
文件夹中,只有 template 与ZTestReport 这2个文件重要,其他的都可以忽略不计。

下载下来后,新建个文件夹,将这两个文件放入到文件夹下。然后手动修改一下ZTestReport的代码。

path 是 指定的生成报告的位置。
templatePath 是 我们下载下来的template的位置,这里需要手动改一下。(为了快速完成,我这里写的是绝对位置,大家要注意改成正确的相对位置)
方法一,我们可以直接对case添加监听。

这种直接在写case的时候,添加监听,
方法二,在xml测试套里添加监听。

二者选其一。
执行case就会自动生成report报告。
这里简单讲一下生成报告的逻辑:
我没看源码,猜的,不对请指出:
template存放的是报告的数据格式,ZTestReport 提供了监听器,将case的运行结果注入到template模版中,然后生成report报告。
我们接下来的二次开发,也要写一个类似于ZTestReport 一样的监听。
下一章: