首页 技术 正文
技术 2022年11月20日
0 收藏 823 点赞 3,139 浏览 2705 个字

一、在进行自动化的过程中,日志一般采用log4j 2进行日志记录,但TestNG自己本身也带有日志记录功能(reporter),它的好处在于日志中记录的内容都是testng自动生成的。

 package testclasses1; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass; public class TestNG_ReportsAndLogs { @BeforeClass
public void setUp() {
// 需要传递2参数(String类型,Boolean)(需要打印的log信息,是否打印在控制台上true or false)
Reporter.log("TestNG_ReportsAndLogs -> 在class开始运行之前运行",true);
} @AfterClass
public void cleanUp() {
Reporter.log("TestNG_ReportsAndLogs -> 在class开始运行之后运行",true);
} @BeforeMethod
public void beforeMethod() {
Reporter.log("TestNG_ReportsAndLogs -> 在test方法开始运行之前运行",true);
} @AfterMethod
public void afterMethod() {
Reporter.log("TestNG_ReportsAndLogs -> 在test方法开始运行之后运行",true);
} @Test
public void testMethod1() {
Reporter.log("TestNG_ReportsAndLogs -> testMethod1",true);
} @Test
public void testMethod2() {
Reporter.log("TestNG_ReportsAndLogs -> testMethod2",true);
Assert.assertTrue(false);
} // 让testMethod3依赖testMethod2
@Test(dependsOnMethods= {"testMethod2"})
public void testMethod3() {
Reporter.log("TestNG_ReportsAndLogs -> testMethod3",true);
}
}

运行截图:

章节十六、10-TestNG报告和日志章节十六、10-TestNG报告和日志

二、如何查看reporter生成的HTML报告

 package testclasses1; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass; public class TestNG_ReportsAndLogs { @BeforeClass
public void setUp() {
// 需要传递2参数(String类型,Boolean)(需要打印的log信息,是否打印在控制台上true or false)
Reporter.log("TestNG_ReportsAndLogs -> 在class开始运行之前运行",true);
} @AfterClass
public void cleanUp() {
Reporter.log("TestNG_ReportsAndLogs -> 在class开始运行之后运行",true);
} @BeforeMethod
public void beforeMethod() {
Reporter.log("TestNG_ReportsAndLogs -> 在test方法开始运行之前运行",true);
} @AfterMethod
public void afterMethod() {
Reporter.log("TestNG_ReportsAndLogs -> 在test方法开始运行之后运行",true);
} @Test
public void testMethod1() {
Reporter.log("TestNG_ReportsAndLogs -> testMethod1",true);
} @Test
public void testMethod2() {
Reporter.log("TestNG_ReportsAndLogs -> testMethod2",true);
Assert.assertTrue(false);
} // 让testMethod3依赖testMethod2
@Test(dependsOnMethods= {"testMethod2"})
public void testMethod3() {
Reporter.log("TestNG_ReportsAndLogs -> testMethod3",true);
}
}

1、首先需要配置xml文件,然后运行

 <!-- 没有此行配置运行时可能会报错 -->
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression TestSuite">
<test name="Application Test">
<classes>
<class name="testclasses1.TestNG_ReportsAndLogs"></class>
</classes>
</test>
</suite>

2、运行结果为:

章节十六、10-TestNG报告和日志

3、xml配置文件运行后会出现如图所示的文件夹

章节十六、10-TestNG报告和日志

4、HTML形式报告展示(截图有限,仅展示一部分)

章节十六、10-TestNG报告和日志

如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴相互一起学习。

内容具有连惯性,未标注的地方可以看前面的博客,这是一整套关于ava+selenium自动化的内容,从java基础开始。

欢迎关注,转载请注明来源。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856