• Extentreport一个测试人员不得不学会的测试报告


    大致简介

    用过很多的测试报告,testng自带的,然后后面自己试着写html代码美化,效果都不是特别的好,直到我发现了现在这款自动化测试报告,简洁直观,UI相对testng来讲不是一个级别的,话不多说,直接上图;

    转载需要注明出处

     
    Extentreport效果图

    如何使用

    那么这么好的一款测试报告如何使用呢?那接下来和我一起探索这一款优秀的测试报告吧~

    首先第一步创建一个maven项目(如何创建请Google或度娘)

    创建好的项目如下:


     
    image.png

    在pom文件中添加相关的引用jar包

    <dependencies>
            <!-- extentreports测试报告插件 -->
            <dependency>
                <groupId>com.relevantcodes</groupId>
                <artifactId>extentreports</artifactId>
                <version>2.41.2</version>
            </dependency>
            <!-- 测试报告插件和testng的结合 -->
            <dependency>
                <groupId>com.vimalselvam</groupId>
                <artifactId>testng-extentsreport</artifactId>
                <version>1.3.1</version>
            </dependency>
            <!-- extentreports测试报告插件 -->
            <dependency>
                <groupId>com.aventstack</groupId>
                <artifactId>extentreports</artifactId>
                <version>3.0.6</version>
            </dependency>
    </dependencies>
    

    点一下右下角的Import Changes,引入(有些同学是用的maven的中央仓库因为被墙了会很慢,需要改成阿里巴巴的会比较快一点,修改的方法很简单自行Google或度娘)


     
    引入jar

    我们来写一点用来测试的代码,如下:

    package com.test.extentreport;
    
    import org.testng.Assert;
    import org.testng.Reporter;
    import org.testng.annotations.Test;
    
    public class TestDemo {
        @Test
        public void test1(){
            Assert.assertEquals(1,2);
        }
    
        @Test
        public void test2(){
            Assert.assertEquals(1,1);
        }
    
    
        @Test
        public void test3(){
            Assert.assertEquals("aaa","aaa");
        }
    
    
        @Test
        public void logDemo(){
            Reporter.log("这是我们自己写的日志");
            throw new RuntimeException("这是我自己的运行时异常");
        }
    }
    
    
    

    写完测试的代码之后进行xml文件的编写

    在resources文件夹新建文件testng.xml,内容如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    <suite name ="testname1">
        <test name="testdemo1">
            <classes>
                <class name="com.tester.extentreports.demo.testDemo">
                    <methods>
                        <inclube name="test1"/>
                        <inclube name="test2"/>
                        <inclube name="test3"/>
                        <inclube name="logDemo"/>
                    </methods>
                </class>
            </classes>
    
        </test>
        <!--配置监听器-->
        <listeners>
            <listener class-name="com.tester.extentreports.demo.ExtentTestNGIReporterListenerOld"/>
        </listeners>
    </suite>
    

    运行,运行完成之后会出现一个如下图的目录


     
    运行完成

    右键单击emailable-report.html,点击copypath


     
    copypath

    打开浏览器粘贴刚刚的path,得到结果没有样式的测试报告


     
    copypath

    删除链接中的 emailable- ,发现还是没有样式,原因是因为我们的样式加载不到,样式文件在“墙外”那么怎么解决呢

     
    更换地址

    首先添加一个工具类(ExtentTestNGIReporterListenerOld.java)

    package com.test.extentreport;
    
    
    import com.aventstack.extentreports.ExtentReports;
    import com.aventstack.extentreports.ExtentTest;
    import com.aventstack.extentreports.ResourceCDN;
    import com.aventstack.extentreports.Status;
    import com.aventstack.extentreports.model.TestAttribute;
    import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
    import com.aventstack.extentreports.reporter.configuration.ChartLocation;
    import com.aventstack.extentreports.reporter.configuration.Theme;
    import org.testng.*;
    import org.testng.xml.XmlSuite;
    
    import java.io.File;
    import java.util.*;
    
    public class ExtentTestNGIReporterListenerOld implements IReporter {
        //生成的路径以及文件名
        private static final String OUTPUT_FOLDER = "test-output/";
        private static final String FILE_NAME = "index.html";
    
        private ExtentReports extent;
    
        

    修改一下代码

     private void init() {
            //文件夹不存在的话进行创建
            File reportDir= new File(OUTPUT_FOLDER);
            if(!reportDir.exists()&& !reportDir .isDirectory()){
                reportDir.mkdir();
            }
            ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
            // 设置静态文件的DNS
            //解决cdn访问不了的问题
            htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
    
            htmlReporter.config().setDocumentTitle("api自动化测试报告");
            htmlReporter.config().setReportName("api自动化测试报告");
            htmlReporter.config().setChartVisibilityOnOpen(true);
            htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
            htmlReporter.config().setTheme(Theme.STANDARD);
            htmlReporter.config().setCSS(".node.level-1  ul{ display:none;} .node.level-1.active ul{display:block;}");
            extent = new ExtentReports();
            extent.attachReporter(htmlReporter);
            extent.setReportUsesManualConfiguration(true);
        }
    

    修改监听器

    <listeners>
            <!-- <listener class-name="com.vimalselvam.testng.listener.ExtentTestNgFormatter"/>-->
            <listener class-name="com.test.extentreport.ExtentTestNGIReporterListenerOld"/>
     </listeners>
    

    修改监听器后运行testng.xml文件会生成index.html


     
    index.html

    copy path


     
    copy path

    在浏览器打开


     
    结果图

    完成!


    原文:https://www.jianshu.com/p/2b036c3cb576

  • 相关阅读:
    聚合物钽电容和普通钽电容的区别
    Java命令:Jstack
    计算并发用户数的五种方法
    LoadRunner脚本优化-加密与解密
    如何定位性能瓶颈
    loadrunner配置多台负载机设置
    nmon监控及简要分析
    Web系统大规模并发——电商秒杀与抢购
    大型网站的灵魂——性能
    mycat实战之性能测试
  • 原文地址:https://www.cnblogs.com/peachh/p/12269701.html
Copyright © 2020-2023  润新知