Product
Solutions
Resources
Company
Download Trial Book a Demo

Code Coverage for Python Applications

Generate code coverage for Python applications with RKTracer. Add the rk4python.py runtime library to your Python installation, instrument your application with rktracer, run your tests, and produce an HTML report.

To generate code coverage for Python-based applications using the RKTracer tool, you need to add the RKTracer runtime library to the Python installation folder, on either the Windows or Linux platform, and instrument the application with the RKTracer tool.

In short

Add the rk4python.py runtime to your Python installation as a one-time setup, instrument your application with rktracer -on, run your tests, then run rkresults to generate the HTML report.

Adding the RKTracer runtime library to your Python installation

Adding the RKTracer runtime library rk4python.py to the Python installation folder is a one-time setup.

On Windows, find where Python is installed. If Python is in the path, then you should see the following output.

find the python path (Windows)
where python
command output
C:\Python39

The command output gives us the path where Python is installed, and you need to copy rk4python.py from the RKTracer installation folder to the Python library folder.

copy the runtime (Windows)
C:\rktracer\share\rktracer  to C:\Python39\Lib

On Linux, find the Python library folder. If Python is in the path, you should see the following output after executing the command below; if nothing displays you see a blank terminal.

find the python path (Linux)
python3 -v 2>&1| grep os.py

Copy rk4python.py from the RKTracer installation folder to the Python library folder.

copy the runtime (Linux)
sudo cp ~/rktracer/share/rktracer/rk4python.py /usr/lib/python3.8/

Now you are all set to instrument your application with the RKTracer tool and generate code coverage.

Step 1: Enable the RKTracer tool

Identify the application folder for which you need code coverage data and instrument it using the RKTracer tool. Execute the following command to turn ON (instrument) the RKTracer tool. The RKTracer tool command will instrument all source files with the py extension.

instrument the application
rktracer -on  path-toproject/module/ py  -v

To turn OFF (un-instrument) the RKTracer tool, run:

un-instrument the application
rktracer -off  path-toproject/module/ py  -v

Step 2: Run your tests on the instrumented application

Run your tests against the instrumented application. Testing can be unit-testing or functional/integration testing of the application.

Once the testing is completed, you should see the rk-coverage.txt file in the testing environment. Copy the rk-coverage.txt file to the application working folder, or the application path where you executed the command rktracer -on path-toproject/module/ py -v to instrument the project folder using the RKTracer tool.

You should also see the rktracer folder in the project work directory. The rktracer folder gets generated at the time of instrumentation. You need to copy the rk-coverage.txt file parallel to the rktracer folder in the project directory.

Step 3: Generate the code coverage reports

Once you have the rk-coverage.txt file, you need to run the rkresults command to generate the code coverage 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.

generate the report
rkresults

The rkresults command will search for the coverage data file rk-coverage.txt and map it with the JSON files (generated during instrumentation) in the rktracer folder, and generate the HTML reports. You can manually open the HTML report using index.html.

Turn the tool off afterwards

After generating code coverage, make sure to turn off (un-instrument) the RKTracer tool for the application that you instrumented.

un-instrument the application
rktracer -off  path-toproject/module/ py  -v

Enable coverage for selected files

Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and want to ignore coverage for the folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder, go to the end of the file, and add the following information.

rktracer.config
ignore *.py
instrument */power/* */core/* */keys/*
never */firmware_loader/* */lockdown/*
  • ignore *.py ignores all Python source files from instrumentation.
  • instrument instruments source files from the given folders.
  • never ignores the selected folder.

Coverage for selected functions

Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder, go to the end of the file, and set the following variables as shown below.

rktracer.config
ignore *.py
instrument *file-X.py *file-Y.py *file-Z.py
function-ignore *
function-instrument fun_X() fun_Y() fun_Z()
  • ignore *.py ignores all Python source files.
  • instrument *file-X.py *file-Y.py *file-Z.py instruments only these three source files.
  • function-ignore * then ignores all functions in the above three files.
  • function-instrument fun_X() fun_Y() fun_Z() does not ignore these three functions from these three files from instrumentation.

Next steps

Once your Python application reports coverage, the same RKTracer workflow applies across other languages and build systems. Browse the rest of the documentation, or read more about how RKTracer measures coverage through MC/DC.

Stuck on your build?

Open a support ticket and an engineer who knows your toolchain will help. You get a tracked ticket ID by email.