Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle named parametrized tests
When using .SetName() on a TestCaseData
the end result in the NUnit results file
won't be using the format of a method call

Issue: JENKINS-5674
  • Loading branch information
timotei committed Nov 8, 2017
1 parent 129accb commit 0e82f81
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 26 deletions.
77 changes: 57 additions & 20 deletions src/main/resources/hudson/plugins/nunit/nunit-to-junit.xsl
Expand Up @@ -3,50 +3,66 @@
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />


<!-- NUnit2 results format -->
<xsl:template match="/test-results">
<testsuites>
<xsl:for-each select="test-suite//results//test-case[1]">

<xsl:for-each select="../..">
<xsl:variable name="firstTestName"
select="results/test-case[1]/@name" />

<xsl:variable name="assembly">
<xsl:choose>
<xsl:when test="substring($firstTestName, string-length($firstTestName)) = ')'">
<xsl:value-of select="substring-before($firstTestName, concat('.', @name))"></xsl:value-of>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(substring-before($firstTestName, @name), @name)" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="testFixtureName">
<xsl:choose>
<!-- we have a classic method name -->
<xsl:when test="substring($firstTestName, string-length($firstTestName)) = ')'">
<xsl:value-of select="substring-before($firstTestName, concat('.', @name))"></xsl:value-of>
</xsl:when>

<!-- we have either a custom name, or a test name -->
<xsl:otherwise>
<xsl:variable name="testMethodName">
<xsl:call-template name="lastIndexOf">
<xsl:with-param name="string" select="$firstTestName" />
<xsl:with-param name="char" select="'.'" />
</xsl:call-template>
</xsl:variable>

<xsl:choose>
<!-- If we didn't find any dot, it means we have just the test name -->
<xsl:when test="$testMethodName=$firstTestName">
<xsl:value-of select="concat(substring-before($firstTestName, @name), @name)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($firstTestName, concat('.', $testMethodName))" />
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--
<xsl:variable name="assembly"
<xsl:variable name="testFixtureName"
select="concat(substring-before($firstTestName, @name), @name)" />
-->

<!-- <redirect:write file="{$outputpath}/TEST-{$assembly}.xml">-->
<!-- <redirect:write file="{$outputpath}/TEST-{$testFixtureName}.xml">-->

<testsuite name="{$assembly}"
<testsuite name="{$testFixtureName}"
tests="{count(*/test-case)}" time="{@time}"
failures="{count(*/test-case/failure)}" errors="0"
skipped="{count(*/test-case[@executed='False' or @result='Inconclusive'])}">
<xsl:for-each select="*/test-case">
<xsl:variable name="testcaseName">
<xsl:choose>
<xsl:when test="contains(./@name, concat($assembly,'.'))">
<xsl:value-of select="substring-after(./@name, concat($assembly,'.'))"/><!-- We either instantiate a "15" -->
<xsl:when test="contains(./@name, concat($testFixtureName,'.'))">
<xsl:value-of select="substring-after(./@name, concat($testFixtureName,'.'))"/><!-- We either instantiate a "15" -->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="./@name"/><!-- ...or a "20" -->
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<testcase classname="{$assembly}"

<testcase classname="{$testFixtureName}"
name="{$testcaseName}">
<xsl:if test="@time!=''">
<xsl:attribute name="time"><xsl:value-of select="@time" /></xsl:attribute>
Expand Down Expand Up @@ -152,4 +168,25 @@ STACK TRACE:

<xsl:template match="properties"/>

<!-- source: https://www.oxygenxml.com/archives/xsl-list/200102/msg00838.html -->
<xsl:template name="lastIndexOf">
<!-- declare that it takes two parameters - the string and the char -->
<xsl:param name="string" />
<xsl:param name="char" />

<xsl:choose>
<!-- if the string contains the character... -->
<xsl:when test="contains($string, $char)">
<xsl:call-template name="lastIndexOf">
<xsl:with-param name="string"
select="substring-after($string, $char)" />
<xsl:with-param name="char" select="$char" />
</xsl:call-template>
</xsl:when>

<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
23 changes: 17 additions & 6 deletions src/test/java/hudson/plugins/nunit/NUnitToJUnitXslTest.java
@@ -1,11 +1,5 @@
package hudson.plugins.nunit;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Transform;
import org.custommonkey.xmlunit.XMLUnit;
Expand All @@ -14,6 +8,12 @@
import org.jvnet.hudson.test.Issue;
import org.xml.sax.InputSource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static org.junit.Assert.assertTrue;

/**
* Unit test for the XSL transformation
*
Expand Down Expand Up @@ -102,6 +102,17 @@ public void testThatNameIsFilledOut() throws Exception {
assertTrue("XSL transformation did not work. " + myDiff, myDiff.similar());
}

@Test
@Issue("JENKINS-5674")
public void namedTestsAreProperlyParsed() throws Exception {
Transform myTransform = new Transform(
new InputSource(this.getClass().getResourceAsStream("NUnit-issue5674-setname.xml")), new InputSource(this
.getClass().getResourceAsStream(NUnitReportTransformer.NUNIT_TO_JUNIT_XSLFILE_STR)));

Diff myDiff = new Diff(readXmlAsString("JUnit-issue5674-setname.xml"), myTransform);
assertTrue("XSL transformation did not work. " + myDiff, myDiff.similar());
}

private String readXmlAsString(String resourceName) throws IOException {
String xmlString = "";

Expand Down
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<testsuites>
<testsuite name="Sample.Tests.SampleTests" tests="3" time="0.017" failures="0" errors="0" skipped="0">
<testcase classname="Sample.Tests.SampleTests" name="first input" time="0.010"/>
<testcase classname="Sample.Tests.SampleTests" name="second input" time="0.000"/>
<testcase classname="Sample.Tests.SampleTests" name="third input" time="0.000"/>
</testsuite>
</testsuites>
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--This file represents the results of running a test suite-->
<test-results name="" total="3" errors="0" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2017-11-06" time="09:40:49">
<environment nunit-version="1.0.0.0" clr-version="4.0.30319.42000" os-version="Microsoft Windows NT 10.0.15063.0" platform="Win32NT" cwd="F:\" machine-name="10" user="t" user-domain="U" />
<culture-info current-culture="en-US" current-uiculture="en-US" />
<test-suite type="Assembly" name="F:\test.dll" executed="True" result="Success" success="True" time="0.027" asserts="3">
<properties>
<property name="_PID" value="14568" />
<property name="_APPDOMAIN" value="test-domain-" />
</properties>
<results>
<test-suite type="TestSuite" name="Sample" executed="True" result="Success" success="True" time="0.021" asserts="3">
<results>
<test-suite type="TestSuite" name="Tests" executed="True" result="Success" success="True" time="0.021" asserts="3">
<results>
<test-suite type="TestFixture" name="SampleTests" executed="True" result="Success" success="True" time="0.019" asserts="3">
<results>
<test-suite type="ParameterizedMethod" name="NamedTestCaseData" executed="True" result="Success" success="True" time="0.017" asserts="3">
<results>
<test-case name="Sample.Tests.SampleTests.first input" executed="True" result="Success" success="True" time="0.010" asserts="1" />
<test-case name="Sample.Tests.SampleTests.second input" executed="True" result="Success" success="True" time="0.000" asserts="1" />
<test-case name="Sample.Tests.SampleTests.third input" executed="True" result="Success" success="True" time="0.000" asserts="1" />
</results>
</test-suite>
</results>
</test-suite>
</results>
</test-suite>
</results>
</test-suite>
</results>
</test-suite>
</test-results>

0 comments on commit 0e82f81

Please sign in to comment.