Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run coverage on tests directory via Travis-CI

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:

enter image description here

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: enter image description here

Whenever I run the coverage for a single file, codecov outputs this:

enter image description here

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)

like image 753
alvarobartt Avatar asked Jan 01 '23 22:01

alvarobartt


1 Answers

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:

.travis.yml file fixed

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!

like image 85
alvarobartt Avatar answered Jan 08 '23 02:01

alvarobartt