[PYTHON] [Django] Test doesn't work if I change the file location

environment

Symptoms

Create an app with python manage.py startapp sample and the test described in sample / tests.py will work. Although I created the sample / tests / directory and sample / tests / test_model.py to split the files, python manage.py test doesn't work.

Execution result

(venv) user@localhost:django-app $ python manage.py test
System check identified no issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

confirmed

Django's tests have ** rules to get test runners to discover them **. I checked if it was aligned correctly.

--Does the test module name (file name) start with test ... ** OK ** ――Do you inherit django.test.TestCase? ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ** OK ** (ʻUnittest.case.Testcaseis also OK) --Does the test method name start withtest? ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ** OK ** --In the first place, is it described in ʻINSTALLED_APPS of settings.py ... ** OK **

Conclusion

The cause was that __init__.py did not exist in thesample / tests /directory. It looks like we need to make Django aware of it as a module. An empty file is fine, so if you create it, it will work.

reference

Recommended Posts

[Django] Test doesn't work if I change the file location
(Note) I'll do a Django tutorial. Even if I follow the site, it doesn't work.
[Django] I wanted to test when POSTing a large file [TDD]
module install if pip install doesn't work
Django doesn't search templates under the application
Create a Python image in Django without a dummy image file and test the image upload
Let's Encrypt updated! What to do if the certbot renew command doesn't work