博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ReportNG测试报告的定制修改
阅读量:4226 次
发布时间:2019-05-26

本文共 5313 字,大约阅读时间需要 17 分钟。

本文系转载 !!!http://blog.csdn.net/qq744746842/article/details/49744647

前段时间在Testerhome上面看到了 简单的描述了一些测试报告的生成,接着有人在评论中回复说可以针对reportNg的测试报告做一些定制化的修改,并且还附上了一张截图。这里我们看下修改后的效果图

这里写图片描述
确实是比reportNg原生的测试报告好看多了。

这里写图片描述,那下来我们就来看看如何去修改我们的reportNg

正文


我们先从github上拉下 reportNg的源代码  , 我们先看下整体的目录结构:

这里写图片描述

因为我们是要修改html的生成,所以说我们大部分要修改的内容都是在resources下进行。

  1.  增加部分类表项 
    这里我们直接在末尾添加

    log=Log Infoscreenshot=Screen Shotduration=Duration   
    1
    2
    3
  2.  修改结果的html,我们目前只修改fail的情况下。

    #if ($failedTests.size() > 0)  
    #foreach ($testClass in $failedTests.keySet())
    #set ($classResults = $failedTests.get($testClass)) #parse ("org/uncommons/reportng/templates/html/class-results.html.vm") #end
    $messages.getString("failedTests")
    $testClass.name $messages.getString("duration") $messages.getString("log") $messages.getString("screenshot")
    #end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
  3.  这里是修改具体的内容显示

    ## Show logger output for the test.    #set ($output = $utils.getTestOutput($testResult))    #if ($output.size() > 0)    
    #foreach( $line in $output ) #if ($meta.shouldEscapeOutput()) $utils.escapeHTMLString($utils.removeImage($line))
    #else $utils.removeImage($line)
    #end #end
    #end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    修改以上部分

          #set ($output = $utils.getTestOutput($testResult))      #if ($output.size() > 0)          
    #foreach( $line in $output ) #if ($meta.shouldEscapeOutput()) $utils.escapeHTMLString($utils.getImageString($line))
    #else $utils.getImageString($line)
    #end #end
    #end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

    新增以上部分 
    上面出现的两个方法getImageString,removeImage。 就是提取含有img标签的字符串和去除带有img标签的字符串

  4.  新增两个方法

    public String getImageString(String s){    String regex = "(
    )"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s); while (matcher.find()) { String group = matcher.group(1); //可根据实际情况多个图片 全部一起return return group; } return "";}public String removeImage(String s){ return s.replaceAll("
    ","");}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    以上就是reportNg修改源代码的位置了

  5. 下来就是我们的测试代码了。实际上很简单 直接一个断言,接着在用例结束的时候判断结果是否失败,是的话就截图。

    @Testpublic void careInterfaceSmoke(){    Assert.assertEquals(1,2);}@AfterMethod(alwaysRun = true)public void afterMethod(ITestResult result) throws Exception {    if (!result.isSuccess())        catchExceptions(result);}public void catchExceptions(ITestResult result) {    System.out.println("result" + result);    String methodName = result.getName();    System.out.println(methodName);    if (!result.isSuccess()) {        File file = new File("");        Reporter.setCurrentTestResult(result);        System.out.println(file.getAbsolutePath());        Reporter.log(file.getAbsolutePath());        String filePath = file.getAbsolutePath();        filePath  = filePath.replace("/opt/apache-tomcat-7.0.64/webapps","http://172.18.44.114:8080");        Reporter.log("");        int width = 100;        int height = 100;        String s = "这是一张测试图片";        File screenShotFile = new File(file.getAbsolutePath()+"/"+result.getName()+".jpg");        Font font = new Font("Serif", Font.BOLD, 10);        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        Graphics2D g2 = (Graphics2D)bi.getGraphics();        g2.setBackground(Color.BLACK);        g2.clearRect(0, 0, width, height);        g2.setPaint(Color.RED);        FontRenderContext context = g2.getFontRenderContext();        Rectangle2D bounds = font.getStringBounds(s, context);        double x = (width - bounds.getWidth()) / 2;        double y = (height - bounds.getHeight()) / 2;        double ascent = -bounds.getY();        double baseY = y + ascent;        g2.drawString(s, (int)x, (int)baseY);        try {            ImageIO.write(bi, "jpg", screenShotFile);        } catch (IOException e) {            e.printStackTrace();        }   
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51

    catchExceptions方法中图片只是为了测试生成了一张图片,实际可以通过对应的测试框架的api方法进行截图操作的。

以上就是基本的内容了,但是还一定要记得在pom.xml配置的时候增加一句

org.apache.maven.plugins
maven-surefire-plugin
2.18.1
false
....
1
2
3
4
5
6
7
8
9
10

上面配置的做用是为了报告中的显示不会单纯的显示出html,而且能够正确的加载出html,生成的报告结果就如下了。

这里写图片描述

总结


感觉上面的方法还是有点取巧的了,感觉真正的方法应该不是如此,不过实在是google了很久,都没有相应的代码。

你可能感兴趣的文章
《计算机系统要素》配套软件和资料下载
查看>>
u-boot 启动过程 —— 基于S3C2410 --转载自周明
查看>>
U-boot大全
查看>>
怎样开公司
查看>>
灵活使用Linux下的glimpse工具和tee命令
查看>>
介绍Linux下经典的文件传输工具: lrzsz
查看>>
活用AXD Debugger调试器的命令行,实现u-boot的源代码级调试
查看>>
程序员的一份礼物-常用工具集
查看>>
uClinux 启动过程详细分析
查看>>
嵌入式系统 Boot Loader 技术内幕
查看>>
通信协议的一些技巧
查看>>
链接命令文件(*.cmd)和gel文件的介绍
查看>>
DSP一点知识总结
查看>>
DSP_c与汇编混合编程的一点总结
查看>>
CamelCase写法
查看>>
高效人士的七个习惯
查看>>
高效人士的七个习惯--每章概括
查看>>
移植U-Boot.1.3.1到S3C244和S3C2410
查看>>
移植u-boot-1.3.4到S3C2440
查看>>
硬件工程师基础知识
查看>>