Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making py.test, coverage and tox work together: __init__.py in tests folder?

I'm having a weird problem with tox, py.test, coverage and pytest-cov: when py.test with the --cov option is launched from tox, it seems to require an __init__.py file in the tests folder which is not immediately obvious.

While writing this post, I have kind of solved the initial problem by adding the aforesaid tests/__init__.py, but to this moment I don't fully understand why exactly it works or doesn't work, so I'm still asking for help. Please see below for details.

I've found a related question on SO but it only makes it more confusing because the answer seems to be opposite to what I've figured out so far: `py.test` and `__init__.py` files

See also the official docs here: py.test - Good Integration Practices (the very bottom of the page).


Simplified project structure:

setup.py tox.ini .coveragerc project/     __init__.py     module1.py     module2.py     tests/         __init__.py (optional, an empty file)         test_module1.py         test_module2.py 

Relevant part of tox.ini:

[testenv:check] commands = py.test --cov=project --cov-report=term deps =     pytest     coverage     pytest-cov  [pytest] python_files = test_*.py norecursedirs = .tox 

Relevant part of .coveragerc:

[run] branch = True omit = project/tests/* 

Now, the results:

  • py.test --cov=project --cov-report=term run from project root => correct coverage whether tests/__init__.py file is present or not.
  • tox -e check without tests/__init__.py => the tests are discovered and run, but I get a warning "Coverage.py warning: No data was collected." and the coverage is 0% for all modules
  • tox -e check with tests/__init__.py => correct coverage again.

It's not immediately obvious to me why the tests/__init__.py file has to be there (adding this empty file solved the initial problem) for the tox run, but it doesn't matter when you run the tests/coverage manually. Any ideas?

like image 863
aldanor Avatar asked Nov 29 '13 20:11

aldanor


People also ask

How do I organize my pytest directory?

The modules containing pytests should be named “test_*. py” or “*_test.py”. While the pytest discovery mechanism can find tests anywhere, pytests must be placed into separate directories from the product code packages. These directories may either be under the project root or under the Python package.

Does coverage work with pytest?

You can use Coverage.py with both unittest and Pytest.

How does Python code coverage work?

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.


1 Answers

Use --cov {envsitepackagesdir}/<your-package-name> in tox.ini.

like image 107
Matt W Avatar answered Sep 21 '22 03:09

Matt W