章节十六、10-TestNG报告和日志
2024-09-01 05:46:31
一、在进行自动化的过程中,日志一般采用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);
}
}
运行截图:
二、如何查看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、运行结果为:
3、xml配置文件运行后会出现如图所示的文件夹
4、HTML形式报告展示(截图有限,仅展示一部分)
如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴相互一起学习。
内容具有连惯性,未标注的地方可以看前面的博客,这是一整套关于ava+selenium自动化的内容,从java基础开始。
欢迎关注,转载请注明来源。
最新文章
- [.Net] 手把手带你将自己打造的类库丢到 NuGet 上
- js修改不了input的值
- [转]基于AWS的自动化部署实践
- Win10 UWP应用发布流程
- opencv的学习笔记4
- dojo.hitch 原理
- stream_iterator、ostream_iterator 使用初探
- angular checkbox required
- jquery-plugin-biggerLink,highLight-层加亮_andy 阳光生活_百度空间
- struts2 taglib struts标签学习整理中
- Swift - 使用arc4random()、arc4random_uniform()取得随机数
- 【个人笔记】《知了堂》前端mySql基础
- a链接在新窗口打开
- 20道Java面试必考题
- [SDOI2008]Sue的小球
- String Byte 互转
- android 图片上传图片 报Socket: Broken pipe
- 【转】RPC介绍
- ArcGisServer根据最大最小坐标换算瓦片行列号【转】
- QtGUI Module&#39;s Classes