I am unable to run coverage.py in the tests directory from a Python project. I have a tests directory containing an __init__.py
file and some test_*.py
files where I define the tests I want to run in the __main__
block of every test file.
The tests directory is located in the same folder as the .travis.yml
file which looks like:
The script block works when I run a single file as coverage run tests/test_errors.py
, but whenever I try to run all the files in the directory it does not work.
I have tried the following solutions:
How can pytest-cov report coverage of python code that is executed as a result of pexpect.spawn? => which did not work for me as Travis-CI debugger said there was no data to combine, and this solution is not excatly what I am looking for, as the most efficient way is not to list all the test files, just the directory I guess.
Python coverage: running more than 1 test => this solution works but the coverage just is made in the tests directory, and I want to test the coverage of the whole project in codecov. To clarify, codecov shows:
Whenever I run the coverage for a single file, codecov outputs this:
Which is what I am looking for, but when it comes to testing multiple Python files contained in the tests directory. Because as you can see, that is the codecov output when coverage run tests/test_investpy.py
not for the whole `tests/' directory.
Any help or information is welcomed! An thank you in advance!
(NOTE: for further information about the project structure you can check the project in https://github.com/alvarob96/investpy as it is an Open Source Project)
After some more research and testing I found out that pytest-cov documentation has an appendix on generating multiple coverage reports for a single test run: https://pytest-cov.readthedocs.io/en/latest/reporting.html
To execute every test contained in tests/
and get the code coverage results in codecov, the script line inside .travis.yml
should be: pytest --cov-report term --cov=investpy tests/
, via pytest you can generate a terminal coverage report for a project specifying the tests/
directory.
So the .travis.yml
when running multiple tests on the same project to combine the code coverage results should be:
So the required packages to get to run the coverage tests on Travis-CI are:
pip install pytest==4.5.0
pip install codecov==2.0.15
pip install pytest-cov==2.7.1
As I already solved it feel free to ask me anything related if you have the same problem and I can help you anyway! If you found this answer useful please rate this answer!
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