1

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?

  • 1
    Is there a file named `__init__.py` inside your `tests` folder? – Toan Quoc Ho Sep 28 '19 at 13:54
  • @ToanQuocHo O_o now I'm startled. Well there was not. The surprising part (for me) was that adding `__init__.py` DID RESOLVE THE ISSUE! I thought python has already removed the need to add empty `__init__.py` files? https://stackoverflow.com/questions/37139786/is-init-py-not-required-for-packages-in-python-3-3 Anyway, thanks! –  Sep 28 '19 at 13:59
  • Let me create an answer and will give you some more information about that. – Toan Quoc Ho Sep 28 '19 at 14:01

1 Answers1

1

Every django-app test-folder needs the following structure to get the tests running properly::

|__django-app
    |__tests
        |-- __init__.py
        |-- test_models.py   
        |-- test_views.py   

Important: Fill in an empty __init__.py File in every test-directory.

So, in your case it should be:

|--- scratchpad
     |--- manage.py
     |--- my_project
          |--- __init__.py
          |--- settings.py
          |--- urls.py
          |--- wsgi.py
          |--- my_app
               |--- __init__.py
               |--- admin.py
               |--- apps.py
               |--- models.py
               |--- tests
                    |--- __init__.py
                    |--- tests_trivial.py
               |--- views.py
               |--- migrations
                    |--- __init__.py

I highly recommend this tutorial William Vincent for beginning with testing with Django Django Testing Tutorial