Ant must perform the following steps in the specified order to analyze your project with Jtest:
- Register the Parasoft build listener.
- Compile the project.
- Run the Jtest task.
Running Static Analysis
To perform static analysis on your code:
- Ensure that the Jtest Plugin for Maven is set up (see Configuring the Jtest Plugin for Ant).
- Ensure that the target you execute has the jtest task is configured in it.
- Add the Parasoft listener to you command line with the
-listener
switch or configure thejtest:listener
task in your target. Build your project with Ant. Your command line may resemble the following:
ant -lib PATH/TO/jtest-ant-plugin.jar -listener com.parasoft.Listener myTarget
The above example the Jtest Plugin for Ant is added to the command line, but you can deploy the JAR file in the Ant's lib directory (see Initial Setup).
Executing Unit Tests
You can include unit test results in the Jtest report by running your tests with the jtest task and the dedicated Unit Tests
built-in test configuration:
- Ensure that the Jtest Plugin for Maven is set up (see Configuring the Jtest Plugin for Ant).
- Ensure that the target you execute to run your tests has the following configured in it:
- the jtest task
- the dedicatedUnit Tests
built-in test configuration - Add the Parasoft listener to you command line with the
-listener
switch or configure thejtest:listener
task in your target. Build your project with Ant. Your command line may resemble the following (the
jtest.coverage.skip
option allows you to skip collecting coverage):ant -lib PATH/TO/jtest-ant-plugin.jar -listener com.parasoft.Listener buildTestAnalyze -Djtest.coverage.skip=true
Collecting Coverage for Unit Tests
You can collect coverage information during execution of unit tests by running your tests with the jtest and agent tasks and the dedicated Unit Tests
built-in test configuration:
- Ensure that the Jtest Plugin for Maven is set up (see Configuring the Jtest Plugin for Ant).
- Ensure that the target you execute to run your tests has the following configured in it:
- the jtest task
- the junit task wrapped with the Jtest agent task
- the dedicatedUnit Tests
built-in test configuration - Add the Parasoft listener to you command line with the
-listener
switch or configure thejtest:listener
task in your target. Build your project with Ant. Your command line may resemble the following:
ant -lib PATH/TO/jtest-ant-plugin.jar -listener com.parasoft.Listener buildTestAnalyze
About the agent
Task
Collecting coverage with the Java agent is the recommended way of collecting coverage data with Jtest. The agent task, which allows runtime bytecode instrumentation (without modification of .class files) must be configured in the appropriate target. The following example shows the build script with the required tasks configured in one target:
<project name="agent-sample" default="buildTestAnalyze" xmlns:jtest="antlib:com.parasoft.jtest.plugin.ant"> <!-- sources directory containing both tests and application source code. Usually these sources are not placed in one directory. --> <property name="sources" value="src"/> <!-- classes directory --> <property name="classes" value="bin"/> <target name="buildTestAnalyze"> <!-- compiling sources --> <javac srcdir="${sources}" destdir="${classes}"/> <!-- instrumenting bytecode --> <!-- run tests --> <jtest:agent> <!-- Following attributes will be set: haltonerror="no" haltonfailure="no" fork="yes" If agent task skipped nested task will be executed without any config changes. --> <junit> <!-- Formatter element will be set as following: <formatter type="xml"/> If different formatter type has been set it will get overridden. If formatter has been configured by classname it will be left as it is. --> <classpath> <pathelement location="${classes}"/> </classpath> <batchtest> <fileset dir="${classes}" includes="**/*Test.class"/> </batchtest> </junit> <coverage> <includes> <fileset dir="${classes}" /> </includes> <testIncludes> <include>**/*Test.class</include> </testIncludes> </coverage> </jtest:agent> <!-- analysis --> <jtest:jtest/> </target> </project>
About the instrument
and instrument-test
Tasks (deprecated)
You can collect coverage for unit tests by running the instrument and instrument-test tasks, which instrument the .class files before test execution. The following example shows the build script with the required tasks configured in one target:
<project name="offline-sample" default="buildInstrumentTestAnalyze" xmlns:jtest="antlib:com.parasoft.jtest.plugin.ant"> <!-- sources directory containing both tests and application source code. Usually these sources are not placed in one directory. --> <property name="sources" value="src"/> <!-- classes directory --> <property name="classes" value="bin"/> <target name="buildTestAnalyze"> <!-- compiling sources --> <javac srcdir="${sources}" destdir="${classes}"/> <!-- instrumenting bytecode --> <jtest:instrument> <fileset dir="${classes}"> <exclude name="**/*Test.class"/> </fileset> <!-- classpathRefId does not have to be specified because of default value <classpathRefId>jtest.classpath</classpathRefId> --> <!-- by default instrumented bytecode overwrites content of ${classes} --> <destDir>${basedir}/parasoft/instrumented-classes</destDir> </jtest:instrument> <jtest:instrument-test> <fileset dir="${classes}"> <include name="**/*Test.class"/> </fileset> <!-- classpathRefId does not have to be specified because of default value <classpathRefId>jtest.classpath</classpathRefId> --> <!-- by default instrumented bytecode overwrites content of ${classes} --> <testDestDir>${basedir}/parasoft/instrumented-classes</testDestDir> </jtest:instrument-test> <!-- run tests with fork option, this is necessary for offline instrumentation, in other case coverage may be incomplete or empty --> <junit haltonfailure="off" haltonerror="off" fork="yes"> <formatter type="xml" /> <classpath> <!-- ${classes} has to be removed from classpath when destDir or testDestDir parameters point to different directory <pathelement location="${classes}"/> --> <!-- Classpath containing instrumented bytecode and libraries required to collect coverage --> <path refid="jtest.classpath"/> </classpath> <batchtest> <fileset dir="${classes}" includes="**/*Test.class"/> </batchtest> </junit> <!-- analysis --> <jtest:jtest/> </target> </project>
Collecting Application Coverage
Jtest's coverage agent allows you to collect coverage data during manual or automated tests performed on a running application. See Application Coverage for information about collecting application coverage with Jtest.
FAQ
How can I disable collecting coverage?
You can disable collecting coverage in one of the following ways:
- Set the
skip
parameter (for bothinstrument
andinstrument-test
tasks if you collect offline coverage) to skip collecting coverage – but not Jtest execution. - Run Ant with the following
-D
option:-Djtest.skip=true
- Configure the following project property:
<property name="jtest.skip" value="true"/>
(make sure the subprojects inherit this property if the Jtest tasks were configured there).
How can I avoid configuring the home parameter in multiple places when using instrument and instrument-test tasks?
- Run Ant with the following
-D
option: -Djtest.home=PATH/TO/JTEST - Configure the following project property: <property name="jtest.home" value="PATH/TO/JTEST"/> (make sure the subprojects inherit this property if the Jtest tasks were configured there).