Summary: Several methods to generate test reports in HTML format
When writing automated tests, a very important task is to generate nice test report.
1. When using junit or testNg, ant can assist generation html format:
<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>
Specific view my other blog post: Project build tools ant's
2. When the management maven test code, test reports can be generated in html format comes with a maven plugin:
Specifically View blog: generate html format test report with plug-maven-surefire-report-plugin
3. xslt format xml, html format to generate
(1) a brief introduction XSLT:
XSLT is an arbitrary text description language for XML document conversion, XSLT T in English on behalf of the "conversion" ( T r ansformation ).
Xslt use xpath to locate elements in the xml file
(2) prepare xml file
For example, the file is 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) prepare xsl file, 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>
Explanation:
Since the XSL stylesheet is itself an XML document, so it is always initiated by the XML declaration: <xml version = "1.0"??>
The root element of the document declared XSL style sheet is <xsl: stylesheet> or <xsl: transform>, To access the XSLT elements, attributes, and characteristics, we must declare the namespace in the top of the document XSLT.
xmlns: xsl = "http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, it must include the attribute version = "1.0".
<Xsl: template> element is used to build templates, and match = "/" attribute put the template associated with the root of the XML source document
Value: <xsl value-of select = "howto / topic" /> element is used to extract a selected node, followed select is used to locate the xml xpath
<Xsl: for-each> element allows you to loop in XSLT.
(4) Prepare java transcoded
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) After executing java code file generation a.html
These are just a simple example, the actual application process is much more complicated
When writing automated tests, a very important task is to generate nice test report.
1. When using junit or testNg, ant can assist generation html format:
<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>
Specific view my other blog post: Project build tools ant's
2. When the management maven test code, test reports can be generated in html format comes with a maven plugin:
Specifically View blog: generate html format test report with plug-maven-surefire-report-plugin
3. xslt format xml, html format to generate
(1) a brief introduction XSLT:
XSLT is an arbitrary text description language for XML document conversion, XSLT T in English on behalf of the "conversion" ( T r ansformation ).
Xslt use xpath to locate elements in the xml file
(2) prepare xml file
For example, the file is 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) prepare xsl file, 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>
Explanation:
Since the XSL stylesheet is itself an XML document, so it is always initiated by the XML declaration: <xml version = "1.0"??>
The root element of the document declared XSL style sheet is <xsl: stylesheet> or <xsl: transform>, To access the XSLT elements, attributes, and characteristics, we must declare the namespace in the top of the document XSLT.
xmlns: xsl = "http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, it must include the attribute version = "1.0".
<Xsl: template> element is used to build templates, and match = "/" attribute put the template associated with the root of the XML source document
Value: <xsl value-of select = "howto / topic" /> element is used to extract a selected node, followed select is used to locate the xml xpath
<Xsl: for-each> element allows you to loop in XSLT.
(4) Prepare java transcoded
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) After executing java code file generation a.html
These are just a simple example, the actual application process is much more complicated