Skip to content

add JUnit report #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion bin/xspec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ usage() {
echo "$1"
echo;
fi
echo "Usage: xspec [-t|-q|-c|-h] filename [coverage]"
echo "Usage: xspec [-t|-q|-c|-j|-h] filename [coverage]"
echo
echo " filename the XSpec document"
echo " -t test an XSLT stylesheet (the default)"
echo " -q test an XQuery module (mutually exclusive with -t)"
echo " -c output test coverage report"
echo " -j output JUnit report"
echo " -h display this help message"
echo " coverage deprecated, use -c instead"
}
Expand Down Expand Up @@ -167,6 +168,13 @@ while echo "$1" | grep -- ^- >/dev/null 2>&1; do
# Coverage
-c)
COVERAGE=1;;
# JUnit report
-j)
if [[ ${SAXON_CP} == *"saxon8"* || ${SAXON_CP} == *"saxon8sa"* ]]; then
echo "Saxon8 detected. JUnit report requires Saxon9."
exit 1
fi
JUNIT=1;;
# Help!
-h)
usage
Expand Down Expand Up @@ -219,6 +227,7 @@ COVERAGE_XML=$TEST_DIR/$TARGET_FILE_NAME-coverage.xml
COVERAGE_HTML=$TEST_DIR/$TARGET_FILE_NAME-coverage.html
RESULT=$TEST_DIR/$TARGET_FILE_NAME-result.xml
HTML=$TEST_DIR/$TARGET_FILE_NAME-result.html
JUNIT_RESULT=$TEST_DIR/$TARGET_FILE_NAME-junit.xml
COVERAGE_CLASS=com.jenitennison.xslt.tests.XSLTCoverageTraceListener

if [ ! -d "$TEST_DIR" ]; then
Expand Down Expand Up @@ -293,6 +302,12 @@ if test -n "$COVERAGE"; then
|| die "Error formating the coverage report"
echo "Report available at $COVERAGE_HTML"
#$OPEN "$COVERAGE_HTML"
elif test -n "$JUNIT"; then
xslt -o:"$JUNIT_RESULT" \
-s:"$RESULT" \
-xsl:"$XSPEC_HOME/src/reporter/junit-report.xsl" \
|| die "Error formating the JUnit report"
echo "Report available at $JUNIT_RESULT"
else
echo "Report available at $HTML"
#$OPEN "$HTML"
Expand Down
87 changes: 87 additions & 0 deletions src/reporter/junit-report.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- =====================================================================

Usage: java -cp "$CP" net.sf.saxon.Transform
-o:"$JUNIT_RESULT" \
-s:"$RESULT" \
-xsl:"$XSPEC_HOME/src/reporter/junit-report.xsl"
Description: XSLT to convert XSpec XML report to JUnit report
Executed from bin/xspec.sh
Input: XSpec XML report
Output: JUnit report
Dependencies: It requires XSLT 3.0 for function fn:serialize()
Authors: Kal Ahmed, github.com/kal
Sandro Cirulli, github.com/cirulls
License: MIT License (https://opensource.org/licenses/MIT)

======================================================================== -->
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:test="http://www.jenitennison.com/xslt/unit-test"
xmlns:pkg="http://expath.org/ns/pkg"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="x xs test pkg xhtml fn">

<xsl:output name="escaped" method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="x:report">
<testsuites>
<xsl:apply-templates select="x:scenario"/>
</testsuites>
</xsl:template>

<xsl:template match="x:scenario">
<testsuite>
<xsl:attribute name="name" select="x:label"/>
<xsl:attribute name="tests" select="count(.//x:test)"/>
<xsl:attribute name="failures" select="count(.//x:test[@successful='false'])"/>
<xsl:apply-templates select="x:test"/>
<xsl:apply-templates select="x:scenario" mode="nested"/>
</testsuite>
</xsl:template>

<xsl:template match="x:scenario" mode="nested">
<xsl:param name="prefix" select="''"/>
<xsl:variable name="prefixed-label" select="concat($prefix, x:label, ' ')"/>
<xsl:apply-templates select="x:test">
<xsl:with-param name="prefix" select="$prefixed-label"/>
</xsl:apply-templates>
<xsl:apply-templates select="x:scenario" mode="nested">
<xsl:with-param name="prefix" select="$prefixed-label"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="x:test">
<xsl:param name="prefix"/>
<testcase>
<xsl:attribute name="name" select="concat($prefix, x:label)"/>
<xsl:attribute name="status">
<xsl:choose>
<xsl:when test="@pending">skipped</xsl:when>
<xsl:when test="@successful='true'">passed</xsl:when>
<xsl:otherwise>failed</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:choose>
<xsl:when test="@pending"><skipped><xsl:value-of select="@pending"/></skipped></xsl:when>
<xsl:when test="@successful='false'">
<failure message="expect assertion failed">
<xsl:apply-templates select="x:expect"/>
</failure>
</xsl:when>
</xsl:choose>
</testcase>
</xsl:template>

<xsl:template match="x:expect[@select]">
<xsl:text>Expected: </xsl:text><xsl:value-of select="x:expect/@select"/>
</xsl:template>

<xsl:template match="x:expect">
<xsl:value-of select="fn:serialize(.)"></xsl:value-of>
</xsl:template>

</xsl:stylesheet>
78 changes: 78 additions & 0 deletions test/xspec-junit.xspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" stylesheet="../src/reporter/junit-report.xsl" xslt-version="3.0">

<x:scenario label="When processing a successful test">
<x:context>
<x:test successful="true">
<x:label>Successful test</x:label>
<x:result>
<p>Foo</p>
</x:result>
<x:expect>
<p>Foo</p>
</x:expect>
</x:test>
</x:context>

<x:expect label="convert it to test case with status passed">
<testcase name="Successful test" status="passed"/>
</x:expect>
</x:scenario>


<x:scenario label="When processing a failing test">
<x:context>
<x:test successful="false">
<x:label>failing test</x:label>
<x:result>
<p>Foo</p>
</x:result>
<x:expect>
<p>Bar</p>
</x:expect>
</x:test>
</x:context>


<x:expect label="convert it to test case with status failed">
<testcase name="failing test"
status="failed">
<failure message="expect assertion failed">&lt;x:expect xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://www.jenitennison.com/xslt/unit-test" xmlns:x="http://www.jenitennison.com/xslt/xspec">&lt;p&gt;Bar&lt;/p&gt;&lt;/x:expect&gt;</failure>
</testcase>
</x:expect>
</x:scenario>


<x:scenario label="When processing successful and failing tests">
<x:context>
<x:test successful="true">
<x:label>Successful test</x:label>
<x:result>
<p>Foo</p>
</x:result>
<x:expect>
<p>Foo</p>
</x:expect>
</x:test>
<x:test successful="false">
<x:label>Failing test</x:label>
<x:result>
<p>Foo</p>
</x:result>
<x:expect>
<p>Bar</p>
</x:expect>
</x:test>
</x:context>

<x:expect label="convert it to test cases with status passed and failing">
<testcase name="Successful test" status="passed"/>
<testcase name="Failing test"
status="failed">
<failure message="expect assertion failed">&lt;x:expect xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://www.jenitennison.com/xslt/unit-test" xmlns:x="http://www.jenitennison.com/xslt/xspec"&gt;&lt;p&gt;Bar&lt;/p&gt;&lt;/x:expect&gt;</failure>
</testcase>
</x:expect>
</x:scenario>


</x:description>
72 changes: 72 additions & 0 deletions test/xspec.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

#!/usr/bin/env bats
#===============================================================================
#
# USAGE: bats xspec.bats
#
# DESCRIPTION: Unit tests for script bin/xspec.sh
#
# INPUT: N/A
#
# OUTPUT: Unit tests results
#
# DEPENDENCIES: This script requires bats (https://github.com/sstephenson/bats)
#
# AUTHOR: Sandro Cirulli, github.com/cirulls
#
# LICENSE: MIT License (https://opensource.org/licenses/MIT)
#
#===============================================================================

@test "invoking xspec without arguments prints usage" {
run ../bin/xspec.sh
[ "$status" -eq 1 ]
[ "${lines[2]}" = "Usage: xspec [-t|-q|-c|-j|-h] filename [coverage]" ]
}


@test "invoking xspec generates XML report file" {
run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec
run stat ../tutorial/xspec/escape-for-regex-result.xml
[ "$status" -eq 0 ]
}

@test "invoking xspec generates HTML report file" {
run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec
run stat ../tutorial/xspec/escape-for-regex-result.html
[ "$status" -eq 0 ]
}

@test "invoking xspec with -j option with Saxon8 returns error message" {
export SAXON_CP=/path/to/saxon8.jar
run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec
[ "$status" -eq 1 ]
[ "${lines[1]}" = "Saxon8 detected. JUnit report requires Saxon9." ]
}


@test "invoking xspec with -j option with Saxon8-SA returns error message" {
export SAXON_CP=/path/to/saxon8sa.jar
run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec
[ "$status" -eq 1 ]
[ "${lines[1]}" = "Saxon8 detected. JUnit report requires Saxon9." ]
}


@test "invoking xspec with -j option generates message with JUnit report location" {
run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec
[ "$status" -eq 0 ]
[ "${lines[18]}" = "Report available at ../tutorial/xspec/escape-for-regex-junit.xml" ]
}

@test "invoking xspec with -j option generates XML report file" {
run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec
run stat ../tutorial/xspec/escape-for-regex-result.xml
[ "$status" -eq 0 ]
}

@test "invoking xspec with -j option generates JUnit report file" {
run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec
run stat ../tutorial/xspec/escape-for-regex-junit.xml
[ "$status" -eq 0 ]
}