When I run coverage for python, I always need an empty __init__.py
file in the tests sub-directory to get coverage to run the tests. This is a requirement for python2 packages, but not for python3. To reproduce, I did the following (pre-requisites are python3, pip3 and brew):
Run the following terminal command:
pip3 install coverage
Create the following directory structure:
example\ example.py tests\ test_example.py
example.py:
#!/usr/bin/env python3
class Example:
value = 3
def update(self):
self.value = 4
test_example.py:
#!/usr/bin/env python3
import unittest
from example.example import Example
class TestExample(unittest.TestCase):
def test_example(self):
example_object = Example()
self.assertEqual(3, example_object.value)
example_object.update()
self.assertEqual(4, example_object.value)
Run the following terminal command:
coverage run --branch -m unittest discover -s . && coverage report
I should get: Ran 1 test in x.yz seconds
, but I always get Ran 0 tests in x.yz seconds
, and to fix this, I have to add __init__.py
files to both directories. How can I run coverage without needing the init files?
Please let me know if you need anything else from me regarding this question. I would appreciate any help!