Running the Cobertura Ant task

This section will explain how to configure the Cobertura Ant task in your project.

The following are the steps for configuration:

  1. Gradle and Maven can download the coverage tool JARs while running the build, but Ant needs the Cobertura JAR files to the classpath. Download the Cobertura ZIP file from http://cobertura.github.io/cobertura/.
  2. Extract the ZIP file and copy all JAR files in the downloaded ZIP to Chapter05lib. Include all JARs from the lib folder and cobertura.jar from the root folder.
  3. Create a build.properties file under Chapter05Chapter05 and enter the following information:
    src.dir=src/main/java
    test.dir=src/test/java
    # The path to cobertura.jar
    cobertura.dir=../lib
    classes.dir=classes
    instrumented.dir=instrumented
    reports.dir=reports
    # Unit test reports from JUnit are deposited into this directory
    reports.xml.dir=${reports.dir}/junit-xml
    reports.html.dir=${reports.dir}/junit-html
    coverage.xml.dir=${reports.dir}/cobertura-xml
    coverage.summaryxml.dir=${reports.dir}/cobertura-summary-xml
    coverage.html.dir=${reports.dir}/cobertura-html
    

    The src.dir attribute represents the source folder location and test.dir represents the test file location. The cobertura.dir attribute refers to the Cobertura library or JAR files. The coverage tool needs to access the Cobertura library files. The other entries are required for report generation and bytecode instrumentation.

  4. Create a build.xml file under Chapter05Chapter05, and add targets for Cobertura instrumentation and JUnit test to update the .ser file and generate the report. Download the build.xml file from the Packt Publishing website (the Chapter05 code). The important targets are init, compile, testcompile, instrument, test, coverage-report, summary-coverage-report, alternate-coverage-report, and clean.
  5. Open the command prompt, change the directory to Chapter05Chapter05, and issue the ant command. This will generate the report. The following is the console output of the command:
    Running the Cobertura Ant task

    Cobertura generates the report in Chapter05Chapter05 eports. The reports folder contains various reports in XML and HTML formats.

    Tip

    Code coverage is not a silver bullet that can deliver zero-defect software! The most important thing is writing effective tests and unit testing the logic. Writing tests for getters and setters or constructor doesn't add value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset