Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python unittests, do I need to import from the tests/__init__.py?

I have my code organized like this:

base_project_dir/src /tests /test1.py /test2.py

in test1.py and test2.py I have the classes extending unittest.TestCase

according to python API doc, I should be able to run

python -m unittest tests from base dir and run all tests. but doing so shows that it finds 0 tests. so I added from test1 import * from test2 import *

into tests/__init__.py

now the above command works. but when I want to run individual tests, it sources the module init, which forcefully run all tests.

what is the correct way to organize this?

thanks Yang

like image 695
teddy teddy Avatar asked Jun 01 '26 16:06

teddy teddy


1 Answers

Try this in your main project dir:

python -m unittest discover -v

This way you don't change the location for your relative imports. When you do python -m unittest tests it switches to this directory first and then can't import the projects code anymore.

like image 97
Michael Avatar answered Jun 04 '26 11:06

Michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!