zoukankan      html  css  js  c++  java
  • 总结:几种生成html格式测试报告的方法

    写自动化测试时,一个很重要的任务就是生成漂亮的测试报告。

    1.用junit或testNg时,可以用ant辅助生成html格式:

    <target name="report" depends="run">

            <junitreport todir="${report.dir}">

                <fileset dir="${report.dir}">

                    <include name="TEST-*.xml" />

                </fileset>

                <report format="noframes" todir="${report.dir}" />

            </junitreport>

            <echo message="Finished running tests." />

        </target>

    具体查看我的另外一篇博客:项目构建工具ant的使用

    2.用maven管理测试代码时,可以用maven自带的插件生成html格式的测试报告:

    具体查看博客:用插件maven-surefire-report-plugin生成html格式测试报告

    3.用xslt格式化xml,生成html格式文件

    (1)简要介绍XSLT:

    XSLT是一种用于将XML文档转换任意文本的描述语言,XSLT中的T代表英语中的“转换”(Transformation)。

    Xslt使用xpath来在xml文件中定位元素

    (2)准备xml文件

    比如说是a.xml文件:

    <?xml version="1.0"?>
    <howto>
      <topic>
          <title>Java</title>
          <url>http://www.java.com</url>
      </topic>
        <topic>
          <title>Python</title>
          <url>http://www.python.com</url>
      </topic>
          <topic>
            <title>Javascript</title>
            <url>http://www.javascript.com</url>
      </topic>
          <topic>
            <title>VBScript</title>
            <url>http://www.VBScript.com</url>
      </topic>
    </howto>

    (3)准备xsl文件,a.xsl:

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
    <html>
    <head><title>Real's HowTo</title></head>
    <body>
    <table border="1">
    <tr>
    <th>Title</th>
    <th>URL</th>
    </tr>
    <xsl:for-each select="howto/topic">
    <tr>
    <td><xsl:value-of select="title"/></td>
    <td><xsl:value-of select="url"/></td>
    </tr>
    </xsl:for-each>
    </table>
    </body></html>
    </xsl:template>
    </xsl:stylesheet>

    解释:

    由于 XSL 样式表本身也是一个 XML 文档,因此它总是由 XML 声明起始:<?xml version="1.0"?>

    把文档声明为 XSL 样式表的根元素是 <xsl:stylesheet> 或 <xsl:transform>,如需访问 XSLT 的元素、属性以及特性,我们必须在文档顶端声明 XSLT 命名空间。

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 指向了官方的 W3C XSLT 命名空间。如果您使用此命名空间,就必须包含属性 version="1.0"。

    <xsl:template> 元素用于构建模板,而 match="/" 属性则把此模板与 XML 源文档的根相联系

    <xsl:value-of select=“howto/topic”/ > 元素用于提取某个选定节点的值,select后面是xpath用来定位xml

    <xsl:for-each> 元素允许您在 XSLT 中进行循环。

    (4).准备java转换代码

    package com.qiuwy.mavenDemo.myTest;

    import javax.xml.transform.*;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
    import java.io.*;

    public class XmlToHtml {
        public static void main(String[] args) {
            String src="a.xml";
            String dest="a.html";
            String xslt="a.xsl";
            
            File src2=new File(src);
            File dest2=new File(dest);
            File xslt2=new File(xslt);
            
            Source srcSource=new StreamSource(src2);
            Result destResult =new StreamResult(dest2);
            Source xsltSource=new StreamSource(xslt2);
            
            try {
                TransformerFactory tFactory = TransformerFactory.newInstance();
                Transformer transformer = tFactory.newTransformer(xsltSource);
                transformer.transform(srcSource, destResult);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    (5)执行java代码后,生成a.html文件

    以上只是一个简单的例子,实际运用过程中还是要复杂很多

  • 相关阅读:
    C++ for循环遍历几种写法
    Windwos查看本地局域网内所有的ip方法
    c define
    ssh
    Linux的iptables菜鸟初学
    nc命令的用法
    电脑莫名重启,VS代码丢失的解决办法
    string判断是否是正常的ip格式
    机器学习之推荐算法
    Springboot将mybatis替换为mybatis-plus
  • 原文地址:https://www.cnblogs.com/weiweiyao/p/4303403.html
Copyright © 2011-2022 走看看