I'm having trouble getting the "coverage" module for Python working correctly.
I have no permissions to modify or add to Python directory, so am unable to install easy_install to setup coverage.
Worked around this by adding the coverage directory to my python path.
Coverage works fine, inputting coverage --version
to shell outputs the version. coverage help
displays all the commands available. Everything seems fine.
I can run coverage fine on small simple scripts.
However, when running a large script with a number of imports across 5 modules (a script that has been tested as working), when running the command with coverage run [normal commands]
, the script executes and fails, because some modules cannot be found.
Here's a breakdown:
Command line without coverage (working fine):
I invoke the script by running: ./script_name.py arg1 arg2 arg3
When I try running the script by inputting: python script_name.py arg1 arg2 arg3
, the script fails, failing to import modules:
ImportError: No module named cx_Oracle
Command line with coverage:
$ coverage run ./script_name.py arg1 arg2 arg3
"No file to run: 'main.py'"
$ coverage run {full_path_to_module_name}/script_name.py arg1 arg2 arg3
Now we are getting somewhere. Coverage is clearly invoked and working, but I get that import error:
ImportError: No module named cx_Oracle
Coverage.py is a tool for measuring code coverage of Python programs. It monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not. Coverage measurement is typically used to gauge the effectiveness of tests.
Increase coverage by adding more tests The code coverage has increased to 78% on adding another test case. It can be increased further to 100% in a similar fashion.
Branch coverage is a metric that indicates whether all branches in a codebase are exercised by tests. A "branch" is one of the possible execution paths the code can take after a decision statement—e.g., an if statement—gets evaluated.
I think I had a similar issue, and managed to solve it by running coverage like this:
python -m coverage run [normal commands]
Specifically in my case it was
python -m coverage run -m unittest discover
It definitely seems to be a case of coverage using a different python installation, as my module that was reported missing was only installed in my virtual env and not in my global env.
The good news is, running with "python" explicitly, and with coverage.py produce the same result. The bad news is, that result is an error message.
It seems like you have more than one Python installation. One is found by "./script_name.py", the other is found by "python ./script_name.py".
To diagnose the problem, add these lines to the top of script_name.py:
import sys
print sys.executable
print "\n".join(sys.path)
This will show you the Python executable being invoked, and the search path for modules. Running your script both ways will show you different results, and you should be able to figure out what is going on.
I will add my experience, as someone could fall into the same mistake.
I was running coverage inside a python virtual environment (venv) with coverage not installed. Executing coverage I was in fact calling the coverage installed globally.
Installing locally with pip3 install coverage
solved it for me, now using python3 -m coverage run myfile.py
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With