Is Viewing JUnit results in TeamCity possible? - java

I'm in the process of setting up a project which uses TeamCity to build the project and run the unit tests.
The first has been done successfully and the project builds. However I can't figure out how to set up TeamCity or my build.xml such that I can view the success or failure of each individual test. Although I'm quite sure I'm attacking this from the wrong angle.
Below is my build.xml:
<project>
<property name="test" location="tests"/>
<target name="test">
<junit printsummary="yes" fork="yes" haltonfailure="yes">
<formatter type="plain"/>
<batchtest fork="true">
<fileset dir="${test}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
</project>

This post on stack overflow answered my main problem. So I'll close this.
Simply generating the ANT build file from within eclipse did the trick.
Now for the reporting...

Related

Fail build on PMD errors/warnings

I have a JAVA ANT project and I am trying to integrate PMD scripts with it so that I can check for all errors and warnings in my project.
Below is snippet of ANT script which I have added in my build.xml:
<property name="pmd.dir" value="buildconfig/build/pmd/" />
<path id="pmd.lib" >
<fileset dir="${pmd.dir}">
<include name="*.jar"/>
<exclude name="/rulesets" />
</fileset>
</path>
<target name="pmd" depends="init">
<echo message="PMD Starting-----" />
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.lib"/>
<pmd shortFilenames="true">
<ruleset>unusedcode</ruleset>
<formatter type="text" toFile="${pmd.dir}/pmd-ant-results.txt"/>
<fileset dir="modules/app/">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
ANT build is working fine with PMD giving proper error reports but I need to abort the build as failure when PMD encounters any errors in code.
I tried adding failOnRuleViolation="yes" but that did not stop build.
Is there anything else I need to add in script?
Try failOnRuleViolation="true" instead of failOnRuleViolation="yes"...
<pmd shortFilenames="true" failOnRuleViolation="true">
...
Some Ant tasks treat true and yes as equivalent, but many tasks don't know how to handle yes at all. It is possible that <pmd> is one of those tasks that doesn't handle yes.
As a rule of thumb, avoid this gotcha in the future by always using true and false instead of yes and no.

JaCoCo JUnit Ant Error : Process Fork Failed

I have an Ant Build file. I added jacoco coverage task to get the coverage of my junit testcases. It says process fork failed.
<jacoco:coverage output="file" destfile="${result.exec.file}">
<!-- forkmode="perTest" printsummary="on" maxmemory="3072m" failureProperty="test.failure" haltonerror="false" includeAntRuntime="true"-->
<junit printsummary="on" fork="true">
<classpath refid="test.run.path" />
<syspropertyset>
<propertyref builtin="commandline" />
</syspropertyset>
<!-- usefile="true" -->
<formatter type="xml" usefile="true" />
<!-- failureproperty="test.failure" haltonerror="false" -->
<batchtest todir="reports/junit-xml" fork="yes">
<fileset dir="src/test/java">
<include name="**/AllTest.java" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
<junitreport todir="${reports.dir}/junit-xml">
<fileset dir="${reports.dir}/junit-xml">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${reports.dir}/junit-html" />
</junitreport>
Have you tried this without jacoco? I ask because I'm having the same problem with fork failure and I'm not using jacoco. I also have another project's ant script doing the same thing and it works fine. Yet somehow the project and ant script I'm currently working on fails the fork. In both cases I have to have fork set to "yes" because I'm specifying an alternate jvm for the junit block.
Maybe the classpath length issue is worth exploring. I know that in my case the ant script that's failing the fork has a longer class path than the one that's working. I had another project with a longer classpath that was also failing to fork but I didn't really need to fork so I just set it to "no".

Java Ant wont complete task after running a Jar

I got some junit tests (in Tester App) that i want to run on a service (Service.jar running parallely). As soon as ant runs Service.jar, it wouldn't return to perform the junit test as expected but just sits there running the Service app. Am i doing something wrong? My build.xml looks like this below..
<target name="compile" depends="clean">
<javac srcdir="${srcdir}" destdir="${testdir}"
verbose="${full-compile}">
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="test" depends="compile,run">
<junit fork="no" haltonfailure="yes">
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="xyz.Tester.Test1" outfile="result" >
<formatter type="plain"/>
<formatter type="xml"/>
</test>
</junit>
</target>
<target name="run">
<java jar="Service.jar" fork="yes">
</java>
</target>
Moreover, i would like to know if there's a way i can run the Service.jar just within Ant (fork="no") if it's possible as ant wouldn't let me (Cannot execute a jar in non-forked mode. Please set fork='true'.)
Any help would be appreciated. Thanks
By default, ant tasks are executed in sequence. When you need to run something in parallel, you need to explicitly use task parallel - see the samples in its docs - something like:
...
<parallel>
<java jar="Service.jar" fork="yes"/>
<sequential>
<sleep seconds="30"/>
<junit fork="true" forkmode="once" ... >
<java jar="ServiceStopper.jar" fork="yes"/>
</sequential>
</parallel>
...
Alternatively, you can use the spawn attribute on the java task. It causes that your service is spawned in a separate process, and processing continues with next ant command.
Concerning the non-forked jar execution - you can avoid forking if you specify classpath and classname attributes.

TFS Can't Find Unit Test Results from Ant/JUnit build

I have a java project that has just been imported into TFS and I've been trying to get TFS to output the results of the unit tests.
At the end of the TFSBuild.proj file I have the following:
<ItemGroup>
<!-- Ant Call Configuration.
The build file called should be included in the workspace of the build definition.
-->
<AntBuildFile Include="$/PROJECT_NAME/Main/tfsbuild.xml">
<Targets>build,test</Targets>
<Properties>BinariesRoot=$(BinariesRoot);BuildDefinitionName=$(BuildDefinitionName);BuildDefinitionUri=$(BuildDefinitionUri);BuildDirectory=$(BuildDirectory);BuildNumber=$(BuildNumber);DropLocation=$(DropLocation);LogLocation=$(LogLocation);SourceGetVersion=$(SourceGetVersion);TestResultsRoot=$(TestResultsRoot);TeamProject=$(TeamProject);WorkspaceName=$(WorkspaceName);WorkspaceOwner=$(WorkspaceOwner)</Properties>
<Lib></Lib>
</AntBuildFile>
<!-- JUnit XML Results files should be created using the XML formatter
and be located in the following path
-->
<JUnitLogFiles Include="$(BinariesRoot)\**\TEST-*.xml" />
</ItemGroup>
This kicks off the build and tells TFS where to find the junit test results.
The issue is, TFS is not finding the unit test results even though I can see through the logs that the tests were run.
I almost gave up on this, and changed my ant file to produce a junit report and store it with the build artifacts. I changed my ant task to be:
<target name="test" depends="compile-tests">
<echo>Running unit tests, output should be in ${junit.output}</echo>
<junit printsummary="yes">
<classpath>
<pathelement path="${compile.classpath}" />
<pathelement path="${lib.dir}/junit-4.0.jar" />
<pathelement path="${build}" />
<pathelement path="${dist-classes}" />
</classpath>
<formatter type="xml" />
<batchtest fork="yes" todir="${junit.output}">
<fileset dir="${src.test}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
<mkdir dir="${DropLocation}/${BuildNumber}/test_results" />
<junitreport todir="${junit.output}">
<fileset dir="${junit.output}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${DropLocation}/${BuildNumber}/test_results" />
</junitreport>
</target>
But after checking the output from the next build, I realised that the JUnit output was saved as TESTS-TestSuites.xml, not TEST-*.xml. I changed my TFSBuild.proj file accordingly, and now have the build results appearing in TFS.
Somehow the junitreport task still picks up the output though.

How to set the working directory for a <junit> task to something other than the basedir?

I have an Ant script with a junit target where I want it to start up the VM with a different working directory than the basedir. How would I do this?
Here's a pseudo version of my target.
<target name="buildWithClassFiles">
<mkdir dir="${basedir}/UnitTest/junit-reports"/>
<junit fork="true" printsummary="yes">
<classpath>
<pathelement location="${basedir}/UnitTest/bin"/>
<path refid="classpath.compile.tests.nojars"/>
</classpath>
<jvmarg value="-javaagent:${lib}/jmockit/jmockit.jar=coverage=:html"/>
<formatter type="xml" />
<test name="GlobalTests" todir="${basedir}/UnitTest/junit-reports" />
</junit>
</target>
Have you tried:
<junit fork="true" printsummary="yes" dir="workingdir">
I think the other answers might be overlooking the fact that you want the working directory to be specified, not just that you want to run junit on a particular directory. In other words, you want to make sure that if a test creates a file with no path information, it is from the base directory you are specifying.
Try to pass in the directory you want as a JVM arg to junit, overriding user.dir:
<junit fork="true" ...>
<jvmarg value="-Duser.dir=${desired.current.dir}"/>
....

Categories

Resources