Put Django Tests in Their Own Folder

Put Django Tests in Their Own Folder

Often you'll end up with several test cases in just one app. Instead of putting all of your tests into one large tests.py file, we can separate them into multiple files within a tests folder. Whether your app continues to grow, requiring more tests, or you just have two or three tests to manage, it's still so much cleaner and ready to scale when we separate out the test cases into their own files.

In fact, this is a great strategy for all of your Django files, whether it's breaking out your models.py or admin.py.

Creating the 'tests' folder

Create a folder called tests inside of your app directory:

my_main_project/
└── my_app/
	└── tests/
	    ├── __init__.py
	    ├── test_integration.py
	    └── ...

The test case module test_integration.py would contain the code for just the integration test cases.

Creating test files

Note that you do need to put 'test_' in front of the file name of your test files. This ensures that Django recognizes it as a test that it needs to run.

Running specific tests

As a bonus by using this structure, you can run specific tests with this command:

python manage.py test my_app.tests.test_integration

This will run the tests found in the test_integration file only.