This is my directory structure:
~
|--- scratchpad
|--- manage.py
|--- my_project
|--- __init__.py
|--- settings.py
|--- urls.py
|--- wsgi.py
|--- my_app
|--- __init__.py
|--- admin.py
|--- apps.py
|--- models.py
|--- tests.py
|--- views.py
|--- migrations
|--- __init__.py
The contents of tests.py
are simple enough:
from django.test import TestCase
# Create your tests here.
class TrivialTestCase(TestCase):
def test_trivial(self):
self.assertTrue(True)
In this setting I can run my tests without problems:
m@mycomp:~/scratchpad$ python3 manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Destroying test database for alias 'default'...
BUT what if the tests.py
file gets so large I'd like to split it into many smaller files AND move them all to a separate folder to keep the app directory clean?
~
|--- scratchpad
|--- manage.py
|--- my_project
|--- __init__.py
|--- settings.py
|--- urls.py
|--- wsgi.py
|--- my_app
|--- __init__.py
|--- admin.py
|--- apps.py
|--- models.py
|--- tests
|--- tests_trivial.py
|--- views.py
|--- migrations
|--- __init__.py
Now manage.py
cannot find my tests!
m@mycomp:~/scratchpad$ python3 manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
I am surprised because this behavior seems to contradict certain highly upvoted answers here, at least if I'm reading these answers right.
Now I can run my tests if I modify my command a little:
m@m-X555LJ:~/scratchpad$ python3 manage.py test my_project.my_app.tests
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Destroying test database for alias 'default'...
However typing all of this each time I want to run tests is a PITA.
Is there any way to make manage.py
discover tests in the tests
subfolder without having to type my_project.my_app.tests
all the time?