简体   繁体   中英

Measure Unit Test coverage of specific files with Py.Test

I'm trying to retrieve the coverage of my unittests with Py.Test. The problem is that I get also coverage information about other python files which I don't care about.

Here is an example of my file structure. In this case I want to test: car.py, wheel.py and steer.py

python2.7/site-packages/

  • car.py
  • wheel.py
  • steer.py
  • coverage.py (from pypi.python.org)
  • yaml (the python yaml library, from pypi.python.org)
  • pytest-2.5.2-py2.7.egg (the python py.test library)
  • and many more

python2.7/site-packages/test/

  • test_car.py
  • conftest.py

Now I use Py.Test to execute the test:

python -m pytest --cov python2.7/site-packages/ python2.7/site-packages/test/test_car.py

But this results in the coverage of all the python files in site-packages (kinda obvious).

python -m pytest --cov python2.7/site-packages/car.py python2.7/site-packages/test/test_car.py

This results in only the coverage of the car.py and not all the files I want.

How can I use Py.Test to only to measure coverage of specific files?

(I also tried:

python -m pytest --cov python2.7/site-packages/car.py,python2.7/site-packages/wheel.py python2.7/site-packages/test/test_car.py

But Py.Test doesn't know how to parse and use this argument. )

I found the solution. You can supply multiple --cov file arguments.

python -m pytest \
  --cov python2.7/site-packages/car.py \
  --cov python2.7/site-packages/wheel.py \
  --cov python2.7/site-packages/steer.py \
  python2.7/site-packages/test/test_car.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM