title: Making py.test, coverage and tox work together: __init__.py in tests folder?
body: <p>I'm having a weird problem with <code>tox</code>, <code>py.test</code>, <code>coverage</code> and <code>pytest-cov</code>: when <code>py.test</code> with the <code>--cov</code> option is launched from <code>tox</code>, it seems to require an <code>__init__.py</code> file in the <code>tests</code> folder which is not immediately obvious.</p>
  
  <p>While writing this post, I have kind of solved the initial problem by adding the aforesaid <code>tests/__init__.py</code>, 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.</p>
  
  <p>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:
  <a href="https://stackoverflow.com/questions/8244277/py-test-and-init-py-files">`py.test` and `__init__.py` files</a></p>
  
  <p>See also the official docs here: <a href="https://pytest.org/latest/goodpractices.html" rel="nofollow noreferrer">py.test - Good Integration Practices</a> (the very bottom of the page).</p>
  
  <hr>
  
  <p>Simplified project structure:</p>
  
  <pre><code>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
  </code></pre>
  
  <hr>
  
  <p>Relevant part of <code>tox.ini</code>:</p>
  
  <pre><code>[testenv:check]
  commands = py.test --cov=project --cov-report=term
  deps =
      pytest
      coverage
      pytest-cov
  
  [pytest]
  python_files = test_*.py
  norecursedirs = .tox
  </code></pre>
  
  <hr>
  
  <p>Relevant part of <code>.coveragerc</code>:</p>
  
  <pre><code>[run]
  branch = True
  omit = project/tests/*
  </code></pre>
  
  <hr>
  
  <p>Now, the results:</p>
  
  <ul>
  <li><code>py.test --cov=project --cov-report=term</code> run from project root => correct coverage whether <code>tests/__init__.py</code> file is present or not.</li>
  <li><code>tox -e check</code> without <code>tests/__init__.py</code> => 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</li>
  <li><code>tox -e check</code> with <code>tests/__init__.py</code> => correct coverage again.</li>
  </ul>
  
  <p>It's not immediately obvious to me why the <code>tests/__init__.py</code> file has to be there (adding this empty file solved the initial problem) for the <code>tox</code> run, but it doesn't matter when you run the tests/coverage manually. Any ideas?</p>