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.
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.
where python
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.
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.
python3 -v 2>&1| grep os.py
Copy rk4python.py from the RKTracer installation folder to the Python library folder.
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.
rktracer -on path-toproject/module/ py -v
To turn OFF (un-instrument) the RKTracer tool, run:
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.
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.
After generating code coverage, make sure to turn off (un-instrument) the RKTracer tool for the application that you instrumented.
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.
ignore *.py instrument */power/* */core/* */keys/* never */firmware_loader/* */lockdown/*
ignore *.pyignores all Python source files from instrumentation.instrumentinstruments source files from the given folders.neverignores 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.
ignore *.py instrument *file-X.py *file-Y.py *file-Z.py function-ignore * function-instrument fun_X() fun_Y() fun_Z()
ignore *.pyignores all Python source files.instrument *file-X.py *file-Y.py *file-Z.pyinstruments 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.