Junit changes:

testAll() xml test case now tests all possible test cases even if one fails

custom JUnit runner is used to send several failures to the notifier

build.xml:
common parameterized tasks are used to run tests and reports for "test" and "test14"

if "outputTestResultsToFile" property is set, a text summary is also generated after testing
  (xsl transformation is using etc/xslt/junit-txt-output.xsl)


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5587 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2007-10-25 23:08:30 +00:00
parent b4e8751acb
commit 1ae3bcd1fc
5 changed files with 125 additions and 60 deletions

View File

@ -162,63 +162,31 @@
<target name="requires-junit" description="Ensures JUnit is available">
<available classname="org.junit.Assert" property="junit.available" classpathref="dependencies.path" />
<fail unless="junit.available" message="Could find JUnit 4... please place a junit.jar file in the CLASSPATH." />
<fail unless="junit.available" message="Could not find JUnit 4... please place a junit.jar file in the CLASSPATH." />
</target>
<condition property="testResultsToFile">
<istrue value="${outputTestResultsToFile}"/>
</condition>
<target name="test" depends="requires-junit,compile,copy" description="Runs the unit tests">
<condition property="testResultsToFile">
<istrue value="${outputTestResultsToFile}"/>
</condition>
<mkdir dir="${dir.build}/reports/xml"/>
<junit printsummary="yes" haltonfailure="no" forkmode="perBatch">
<classpath>
<path refid="dependencies.path" />
</classpath>
<batchtest fork="yes" todir="${dir.build}/reports/xml">
<fileset dir="${dir.regress}">
<include name="test/**/*Test.java" />
<exclude name="test/**/Abstract*"/>
</fileset>
</batchtest>
<formatter type="xml" if="testResultsToFile"/>
</junit>
<junitreport todir="${dir.build}/reports/xml">
<fileset dir="${dir.build}/reports/xml">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${dir.build}/reports/html" />
</junitreport>
<antcall target="test-execute">
<param name="test.outputdir" value="${dir.build}/reports"/>
<param name="test.jvm" value="java"/>
<param name="test.dependencies" value="dependencies.path"/>
</antcall>
<antcall target="test-report">
<param name="test.outputdir" value="${dir.build}/reports"/>
</antcall>
</target>
<target name="test14" depends="requires-junit,compile,copy,weavejunit,weave" description="Runs the unit tests with 1.4 jvm">
<fail unless="jvm14.exe" message="jvm14.exe location is not defined properly" />
<condition property="testResultsToFile">
<istrue value="${outputTestResultsToFile}"/>
</condition>
<path id="dependencies14.path">
<fileset dir="${dir.lib14}">
<include name="backport-util-concurrent.jar" />
<include name="pmd14-${version}.jar" />
<include name="pmd14-test-${version}.jar" />
<include name="retroweaver-rt-${retroweaver.version}.jar" />
<include name="junit.jar" />
</fileset>
<fileset dir="${dir.lib}">
<include name="jaxen-1.1.1.jar" />
<include name="asm-3.0.jar" />
</fileset>
</path>
<mkdir dir="${dir.build}/reports14/xml"/>
<junit printsummary="yes" haltonfailure="no" jvm="${jvm14.exe}" forkmode="perBatch">
<target name="test-execute" description="Runs the unit tests">
<mkdir dir="${test.outputdir}/xml"/>
<junit printsummary="yes" haltonfailure="no" jvm="${test.jvm}" forkmode="perBatch">
<classpath>
<path refid="dependencies14.path" />
<path refid="${test.dependencies}" />
</classpath>
<batchtest fork="yes" todir="${dir.build}/reports14/xml">
<batchtest fork="yes" todir="${test.outputdir}/xml">
<fileset dir="${dir.regress}">
<include name="test/**/*Test.java" />
<exclude name="test/**/Abstract*"/>
@ -227,12 +195,46 @@
<formatter type="xml" if="testResultsToFile"/>
</junit>
<junitreport todir="${dir.build}/reports14/xml">
<fileset dir="${dir.build}/reports14/xml">
</target>
<target name="test-report" if="testResultsToFile" description="generate reports">
<junitreport todir="${test.outputdir}/xml">
<fileset dir="${test.outputdir}/xml">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${dir.build}/reports14/html" />
<report format="frames" todir="${test.outputdir}/html" />
</junitreport>
<xslt in="${test.outputdir}/xml/TESTS-TestSuites.xml" style="etc/xslt/junit-txt-output.xsl" out="${test.outputdir}/summary.txt" />
<concat>
<filelist dir="${test.outputdir}" files="summary.txt"/>
</concat>
</target>
<path id="dependencies14.path">
<fileset dir="${dir.lib14}">
<include name="backport-util-concurrent.jar" />
<include name="pmd14-${version}.jar" />
<include name="pmd14-test-${version}.jar" />
<include name="retroweaver-rt-${retroweaver.version}.jar" />
<include name="junit.jar" />
</fileset>
<fileset dir="${dir.lib}">
<include name="jaxen-1.1.1.jar" />
<include name="asm-3.0.jar" />
</fileset>
</path>
<target name="test14" depends="requires-junit,compile,copy,weavejunit,weave" description="Runs the unit tests with 1.4 jvm">
<fail unless="jvm14.exe" message="jvm14.exe location is not defined properly" />
<antcall target="test-execute">
<param name="test.outputdir" value="${dir.build}/reports14"/>
<param name="test.jvm" value="${jvm14.exe}"/>
<param name="test.dependencies" value="dependencies14.path"/>
</antcall>
<antcall target="test-report">
<param name="test.outputdir" value="${dir.build}/reports14"/>
</antcall>
</target>
<target name="symtabtest" depends="compile" description="Runs the unit tests for the symboltable package">

View File

@ -0,0 +1,22 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="testsuites">
Junit test results
<xsl:for-each select="testsuite[(@failures + @errors) &gt; 0]">
Test suite <xsl:value-of select="@name"/> failed (failures: <xsl:value-of select="@failures"/>, errors: <xsl:value-of select="@errors"/>)
<xsl:for-each select="testcase[(count(error) + count(failure)) &gt; 0]">
Test case <xsl:value-of select="@name"/> failed (failures: <xsl:value-of select="count(failure)"/>, errors: <xsl:value-of select="count(error)"/>)
<xsl:for-each select="failure">
failure: <xsl:value-of select="@message"/>.
</xsl:for-each>
<xsl:for-each select="error">
error: <xsl:value-of select="@message"/>.
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
Summary
failures: <xsl:value-of select="count(//failure)"/>, errors: <xsl:value-of select="count(//error)"/>.
</xsl:template>
</xsl:stylesheet>

View File

@ -235,8 +235,7 @@ public Object clone() throws CloneNotSupportedException {
<test-code>
<description>
<![CDATA[
False +: Overriding method merely calls super
See bug 1415525
False +: Overriding method merely calls super (see bug 1415525)
]]>
</description>
<expected-problems>0</expected-problems>

View File

@ -82,9 +82,9 @@ public abstract class RuleTst {
res = processUsingStringReader(test.getCode(), rule, test.getSourceType()).size();
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException("Test \"" + test.getDescription() + "\" on Rule \"" + test.getRule().getName() + "\"failed");
throw new RuntimeException('"' + test.getDescription() + "\" failed");
}
assertEquals("Test \"" + test.getDescription() + "\" on Rule \"" + test.getRule().getName() + "\" resulted in wrong number of failures,",
assertEquals('"' + test.getDescription() + "\" resulted in wrong number of failures,",
test.getNumberOfProblemsExpected(), res);
} finally {
//Restore old properties

View File

@ -6,13 +6,19 @@ package test.net.sourceforge.pmd.testframework;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import net.sourceforge.pmd.Rule;
import org.junit.Test;
import org.junit.internal.runners.TestClassMethodsRunner;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
/**
* Standard methods for (simple) testcases.
*/
@RunWith(SimpleAggregatorTst.CustomXmlTestClassMethodsRunner.class)
public abstract class SimpleAggregatorTst extends RuleTst {
/**
* Run a set of tests defined in an XML test-data file for a rule. The file
@ -56,9 +62,45 @@ public abstract class SimpleAggregatorTst extends RuleTst {
*/
@Test
public void testAll() {
ArrayList<Failure> l = new ArrayList<Failure>();
for (Rule r : rules) {
runTests(r);
TestDescriptor[] tests = extractTestsFromXml(r);
for (int i = 0; i < tests.length; i++) {
try {
runTest(tests[i]);
} catch (Throwable t) {
Failure f = CustomXmlTestClassMethodsRunner.createFailure(r, t);
l.add(f);
}
}
}
for(Failure f: l) {
CustomXmlTestClassMethodsRunner.addFailure(f);
}
}
public static class CustomXmlTestClassMethodsRunner extends TestClassMethodsRunner {
public CustomXmlTestClassMethodsRunner(Class<?> klass) {
super(klass);
}
public static Failure createFailure(Rule rule, Throwable targetException) {
return new Failure(Description.createTestDescription(
SimpleAggregatorTst.class, "xml." + rule.getRuleSetName() + '.' + rule.getName()),
targetException);
}
public static void addFailure(Failure failure) {
NOTIFIER.fireTestFailure(failure);
}
@Override
public synchronized void run(RunNotifier n) {
// synchronized so that access to NOTIFIER is safe
NOTIFIER = n;
super.run(n);
}
private static RunNotifier NOTIFIER;
}
}