25

My project has a structure like:

root/
  .coveragerc
  folder_a/
  folder_b/
  tests/
    folder_a/
    folder_b/

I would like to run coverage against folder_a and folder_b. Is possible to run it in a single command? Something like pytest --cov=* tests/ --cov-report html...

4
  • 7
    pytest --cov=folder_a --cov=folder_b?
    – hoefling
    Aug 19, 2019 at 12:15
  • Oh cool. Is possible to get folders name dynamically?
    – user866364
    Aug 19, 2019 at 12:16
  • 1
    I'm not sure what you mean by "dynamically" - if you're looking for a glob pattern like --cov=folder_*, then it depends on your shell whether it's expanded before passing the argument. --cov itself doesn't expand globs. For example, --cov=folder_{a,b} should work in bash, while --cov=folder_* not.
    – hoefling
    Aug 19, 2019 at 12:20
  • hoefling's comment is the only one that worked for me
    – marvin
    Jun 8, 2020 at 15:06

3 Answers 3

29

I'm posting the answer from @hoefling here because I didn't see the answer in the comment at first and saw it later when I looked at all the comments.

To specify coverage for multiple folders, provide the --cov multiple times:

pytest --cov=folder_a --cov=folder_b --cov=folder_c
1
  • Note that the documentation shows a syntax where you specify the path of the source AND the relative path of the tests within a single --cov argument. This appears to not be necessary and causes it to not work for me - just adding each source path as shown in the answer here does the trick. Mar 13, 2022 at 22:15
1

Yes it is possible.

For example,

for a structure,

 > proj_folder
   - > tests_functional
   - > tests_unit

Example

$ pytest --cov=proj_folder proj_folder/tests_unit proj_folder/tests_functional

Syntax

$ pytest --cov=<proj_folder/package_name> <proj_folder>/tests_unit <proj_folder>/tests_functional
0

I found this to work well. It runs coverage.py using pytest:

coverage run -m pytest

This was run at the project fodler. It was able to find the folder called tests, and run all the tests (which was split into multiple python files) within them. To see the report run:

coverage report

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.