You will learn how to generate a code coverage ANT build Java applications using the RKTracer Tool in 3 simple steps.
You need to prefix rktracer command to ant build command to enable code coverage tool. Runtime Jar will be added to the build system by the RKTracer. Tests can be run on an RKTracer instrumented application on a native host or on server machine and code coverage can be generated.
RKTracer tool makes internal adjustments to ANT build to instrument all the source files compiled at build time. By default, rktracer instruments for multiple-condition code coverage, and you see the following code coverage information in the HTML report.
- Function Coverage
- Line Coverage
- Statement Coverage
- Multi-Condition Code Coverage
Prefix rktracer to the ant build command to instrument the source files, run your tests, then run rkresults to generate the HTML report.
Code Coverage ANT build example
Here is an example application to demonstrate how code coverage can be generated with ANT build system.
Enable RKTracer Tool
To enable the coverage tool, prefix the rktracer command to your ant build command. For example, change your original build command:
ant clean compile jar
to the following, prefixing rktracer (here with the verbose flag -v):
rktracer -v ant clean compile jar
RKTracer instruments the java source file with c:\rktracer/lib/librklij-x86_64-w64-mingw32.exe and writes the instrumented file to the prime.java file in a preprocessed directory. Also, it stores the metadata, structure of instrumentation, and copy of source file content in JSON file. The tool will refer rktracer.config for internal configuration at the time of instrumentation.
RKTracer automatically adds the runtime library to ANT installation.
Run tests on instrumented application
You can run unit tests or integration tests in native host machine or server machine. In this case, we will run unit tests and functional tests.
You can also see coverage metrics written out to the rk-coverage.txt file.
Generate Code Coverage HTML reports
Once you have the rk-coverage.txt file, you need to run the command rkresults to generate HTML reports. Ensure that you run the rkresults command in the application root folder or where you have the rktracer folder generated at the time of build/Instrumentation.
rkresults
rkresults command will search coverage data file rk-coverage.txt and map with JSON files (generated during instrumentation) in the rktracer folder and generate an Html report.
You can manually open the HTML report using index.html.
Enable coverage for selected files
You can scope coverage to specific folders, files, or functions by editing rktracer.config in the RKTracer installation folder.
Generate coverage reports for the selected folders
Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and ignore coverage for folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder and go to the end of the file add the following information.
ignore *.java instrument */power/* */core/* */keys/* never */firmware_loader/* */lockdown/*
ignore *.java= Ignores all Java source files from instrumentationinstrument= Instrument source files from given foldersnever= ignore selected folder
Generate coverage reports only for selected functions from three different files
Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder and go to the end of the file and set the following variables as shown below.
ignore *.java instrument *source-file-X.java *source-file-Y.java *source-file-Z.java function-ignore * function-instrument fun_X() fun_Y() fun_Z()
ignore *.java= Ignore all Java programming source filesinstrument= Instrument only these three source filesfunction-ignore *= Then ignore all functions in the above three filesfunction-instrument= Don't ignore these three functions from instrumentation