Python not able to find tests when test files are in different module
Python not able to find tests when test files are in different module
I have a directory structure like below
horizontalupgrade
common/
__init__.py
upgradestate.py
tests/
common/
__init__.py
testupgradestate.py
Content of testupgradestate.py
testupgradestate.py
import unittest
from upgradestate import UpgradeState
class UpgradeStateTest(unittest.TestCase):
def setUp(self):
print "Setup Called"
def test_copy(self):
u = UpgradeState("")
print "test_copy Called"
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(UpgradeStateTest)
runner = unittest.TextTestRunner()
runner.run(suite)
But on trying to execute the tests python is not able to find the tests
(venv) dmanna-a01:horizontalupgrade dmanna$ python -m unittest discover -v
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
But if I make my directory structure like below
horizontalupgrade
common/
__init__.py
upgradestate.py
testupgradestate.py
Then the tests are running fine
(venv) dmanna-a01:horizontalupgrade dmanna$ python -m unittest discover -v
test_copy (common.testupgradestate.UpgradeStateTest) ... Setup Called
test_copy Called
ok
----------------------------------------------------------------------
Ran 1 tests in 0.000s
OK
Can someone let me know what I am doing wrong? How can I make the tests run from a different test package?
2 Answers
2
You need an __init__.py
in tests/
. Unittest's discovery only works on packages.
https://docs.python.org/3/library/unittest.html
__init__.py
tests/
Try this, Add these code in the beginning of yours:
import sys
sys.path.append('.../common')
Or you can use absolute path in append.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.