2

If I run a set of unit tests in Android Studio (would apply in IntelliJ IDEA as well) then I am able to export the test results using this dialog:

export test results dialog

If I choose "Export format/HTML" I get a file that renders like this:

test report html

which is all very well. However, I have a requirement where I must present the test report in a Word file with a template. If I copy/paste the default HTML in the browser it seems the HTML is too complex for Word to handle correctly and it doesn't look good.

result of copying into Word

I need some kind of plain text representation of the test report that I can copy into Word/Excel and include as part of a report.

As can be seen on the dialog box above, one of the options is to apply a XSL template to generate results. I would like to write a XSL template to render the JUnit unit test report as something simple that I can copy/paste easily. Note there is no XSL template provided by default with Android Studio or IntelliJ IDEA.

If I export the XML for a minified JUnit test report it looks like this:

<?xml version="1.0" encoding="UTF-8"?><testrun duration="1" footerText="Generated by Android Studio on 4/05/17 12:43 PM" name="myapplication in app">
    <count name="total" value="3"/>
    <count name="passed" value="3"/>
    <config nameIsGenerated="true" configId="AndroidJUnit" name="myapplication in app">
        <extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea"/>
        <module name="app"/>
        <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false"/>
        <option name="ALTERNATIVE_JRE_PATH"/>
        <option name="PACKAGE_NAME"/>
        <option name="MAIN_CLASS_NAME"/>
        <option name="METHOD_NAME"/>
        <option name="TEST_OBJECT" value="directory"/>
        <option name="VM_PARAMETERS"/>
        <option name="PARAMETERS"/>
        <option name="WORKING_DIRECTORY"/>
        <option name="ENV_VARIABLES"/>
        <option name="PASS_PARENT_ENVS" value="true"/>
        <option name="TEST_SEARCH_SCOPE">
            <value defaultName="singleModule"/>
        </option>
        <envs/>
        <dir value="/Users/example/AndroidStudioProjects/MyApplication5/app/src/test/java/com/example/myapplication"/>
        <patterns/>
    </config>
    <root name="&lt;default package&gt;" location="java:suite://&lt;default package&gt;"/>
    <suite duration="1" locationUrl="java:suite://com.example.myapplication.ArithmeticTest" name="ArithmeticTest" status="passed">
        <test duration="1" locationUrl="java:test://com.example.myapplication.ArithmeticTest.multiplication_isCorrect" name="ArithmeticTest.multiplication_isCorrect" status="passed"/>
        <test duration="0" locationUrl="java:test://com.example.myapplication.ArithmeticTest.addition_isCorrect" name="ArithmeticTest.addition_isCorrect" status="passed"/>
    </suite>
    <suite duration="0" locationUrl="java:suite://com.example.myapplication.StringTest" name="StringTest" status="passed">
        <test duration="0" locationUrl="java:test://com.example.myapplication.StringTest.concatenation_isCorrect" name="StringTest.concatenation_isCorrect" status="passed"/>
    </suite>
</testrun>

Question: What is an XSL template I can apply to get a transformation of the XML into a very plain HTML that I can copy/paste into a Word file for the company?

Note: presented as a self-answered question but other answers are welcome and will be accepted if good quality

David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • Related but out of date and localised (5 years old, perhaps for an earlier version of JUnit) : http://stackoverflow.com/questions/9470060/what-xslt-converts-junit-xml-format-to-junit-plain-format – David Rawson May 04 '17 at 01:17

1 Answers1

1

This is my best attempt given the time constraints and my lack of knowledge of XSL.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <table border="1" style="width:100%">
          <th>TestRun</th>
          <th>Total</th>
          <th>Passed</th>
          <tr>
            <td>
              <xsl:value-of select="concat(testrun/@name, ' - ',  testrun/@footerText)"/>
            </td>
            <td>
              <xsl:value-of select="testrun/count[1]/@value"/>
            </td>
            <td>
              <xsl:value-of select="testrun/count[2]/@value"/>
            </td>
          </tr>
        </table>
        <xsl:for-each select="testrun/suite">
          <table border="1" style="width:100%">
            <th>
              <xsl:value-of select="@name"/>
            </th>
            <th></th>
            <th></th>
            <xsl:for-each select="test">
              <tr>
                <td>
                  <xsl:value-of select="@name"/>
                </td>
                <td>
                  <xsl:value-of select="@duration"/>ms</td>
                <td>
                  <xsl:value-of select="@status"/>
                </td>
              </tr>
            </xsl:for-each>
          </table>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

As required, it generates a very basic HTML that can copy and pasted into Office.

David Rawson
  • 20,912
  • 7
  • 88
  • 124