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

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

 1 package testclasses1;
 2 
 3 import org.testng.annotations.Test;
 4 import org.testng.annotations.BeforeMethod;
 5 import org.testng.annotations.AfterMethod;
 6 import org.testng.annotations.BeforeClass;
 7 import org.testng.Assert;
 8 import org.testng.Reporter;
 9 import org.testng.annotations.AfterClass;
10 
11 public class TestNG_ReportsAndLogs {
12     
13     @BeforeClass
14     public void setUp() {
15 //        需要传递2参数(String类型,Boolean)(需要打印的log信息,是否打印在控制台上true or false)
16         Reporter.log("TestNG_ReportsAndLogs -> 在class开始运行之前运行",true);
17     }
18     
19     @AfterClass
20     public void cleanUp() {
21         Reporter.log("TestNG_ReportsAndLogs -> 在class开始运行之后运行",true);
22     }
23     
24     @BeforeMethod
25     public void beforeMethod() {
26         Reporter.log("TestNG_ReportsAndLogs -> 在test方法开始运行之前运行",true);
27     }
28     
29     @AfterMethod
30     public void afterMethod() {
31         Reporter.log("TestNG_ReportsAndLogs -> 在test方法开始运行之后运行",true);
32     }
33     
34   @Test
35   public void testMethod1() {
36         Reporter.log("TestNG_ReportsAndLogs -> testMethod1",true);
37   }
38 
39   @Test
40   public void testMethod2() {
41         Reporter.log("TestNG_ReportsAndLogs -> testMethod2",true);
42         Assert.assertTrue(false);
43   }
44   
45 //  让testMethod3依赖testMethod2
46   @Test(dependsOnMethods= {"testMethod2"})
47   public void testMethod3() {
48         Reporter.log("TestNG_ReportsAndLogs -> testMethod3",true);
49   }
50 }

运行截图:

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

 1 package testclasses1;
 2 
 3 import org.testng.annotations.Test;
 4 import org.testng.annotations.BeforeMethod;
 5 import org.testng.annotations.AfterMethod;
 6 import org.testng.annotations.BeforeClass;
 7 import org.testng.Assert;
 8 import org.testng.Reporter;
 9 import org.testng.annotations.AfterClass;
10 
11 public class TestNG_ReportsAndLogs {
12     
13     @BeforeClass
14     public void setUp() {
15 //        需要传递2参数(String类型,Boolean)(需要打印的log信息,是否打印在控制台上true or false)
16         Reporter.log("TestNG_ReportsAndLogs -> 在class开始运行之前运行",true);
17     }
18     
19     @AfterClass
20     public void cleanUp() {
21         Reporter.log("TestNG_ReportsAndLogs -> 在class开始运行之后运行",true);
22     }
23     
24     @BeforeMethod
25     public void beforeMethod() {
26         Reporter.log("TestNG_ReportsAndLogs -> 在test方法开始运行之前运行",true);
27     }
28     
29     @AfterMethod
30     public void afterMethod() {
31         Reporter.log("TestNG_ReportsAndLogs -> 在test方法开始运行之后运行",true);
32     }
33     
34   @Test
35   public void testMethod1() {
36         Reporter.log("TestNG_ReportsAndLogs -> testMethod1",true);
37   }
38 
39   @Test
40   public void testMethod2() {
41         Reporter.log("TestNG_ReportsAndLogs -> testMethod2",true);
42         Assert.assertTrue(false);
43   }
44   
45 //  让testMethod3依赖testMethod2
46   @Test(dependsOnMethods= {"testMethod2"})
47   public void testMethod3() {
48         Reporter.log("TestNG_ReportsAndLogs -> testMethod3",true);
49   }
50 }

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

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

2、运行结果为:

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

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

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

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

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

猜你喜欢

转载自www.cnblogs.com/luohuasheng/p/11578842.html
今日推荐