Skip to content

Instantly share code, notes, and snippets.

@aemergent
Created February 9, 2025 08:58
Show Gist options
  • Save aemergent/7f9414e19f464868b59804d189b2aea4 to your computer and use it in GitHub Desktop.
Save aemergent/7f9414e19f464868b59804d189b2aea4 to your computer and use it in GitHub Desktop.
django_testing_playbook.md

Django Test Execution Playbook

This playbook provides examples of running Django tests at different levels (module, class, and function) using different configurations.

Test Environment

  • Python Environment: /root/miniconda3/envs/django_env/bin/python
  • Test Runner: tests/runtests.py
  • Default Settings: test_sqlite

Test Execution Patterns

1. Running a Single Test Function

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --settings=test_sqlite test_runner.test_parallel.RemoteTestResultTest.test_picklable

This runs a specific test function within a test class.

2. Running a Test Class

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --settings=test_sqlite test_runner.test_parallel.RemoteTestResultTest

This runs all test functions within a specific test class.

3. Running a Full Test Module

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --settings=test_sqlite test_runner.test_parallel

This runs all test classes within a module.

4. Running Tests with Verbosity

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --verbosity=2 --settings=test_sqlite test_runner

Verbosity levels:

  • 0: minimal output
  • 1: normal output (default)
  • 2: verbose output
  • 3: very verbose output (shows individual test names)

5. Running Tests in Parallel

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --parallel=2 --settings=test_sqlite admin_views

This runs tests in parallel using multiple processes.

Test Results Log

Single Test Function Example

Command:

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --settings=test_sqlite test_runner.test_parallel.RemoteTestResultTest.test_picklable

Results:

  • Total tests: 1
  • Passed: 1
  • Skipped: 0
  • Failed: 0
  • Time taken: ~0.005s Notes: Example of running a single test function

Test Class Example

Command:

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --settings=test_sqlite test_runner.test_parallel.RemoteTestResultTest

Results:

  • Total tests: 13
  • Passed: 7
  • Skipped: 6
  • Failed: 0
  • Time taken: ~0.010s Notes: Example of running all tests in a class

Full Module Example

Command:

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --settings=test_sqlite test_runner.test_parallel

Results:

  • Total tests: 17
  • Passed: 11
  • Skipped: 6
  • Failed: 0
  • Time taken: ~0.010s Notes: Example of running all tests in a module

Database Functions Module

Command:

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --verbosity=2 --settings=test_sqlite db_functions.tests

Results:

  • Total tests: ~350
  • Passed: ~350
  • Skipped: 0
  • Failed: 0 Notes: Tests database functions and transformations

Template Context Module

Command:

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --verbosity=2 --settings=test_sqlite template_tests.test_context

Results:

  • Total tests: ~25
  • Passed: ~25
  • Skipped: 0
  • Failed: 0 Notes: Tests template context functionality including RequestContext

Forms Module

Command:

/root/miniconda3/envs/django_env/bin/python tests/runtests.py --settings=test_sqlite forms_tests.tests.test_forms

Results:

  • Total tests: ~270
  • Passed: ~147
  • Skipped: ~123
  • Failed: 0 Notes: Tests form functionality and validation

Important Notes

  1. Test counts may vary slightly between Django versions
  2. Some tests may be skipped if optional dependencies are not installed (like tblib for test_runner tests)
  3. Use --verbosity=3 to see individual test names when debugging
  4. Always run tests from the project root directory
  5. The test database is automatically created and destroyed for each test run

Common Issues and Solutions

  1. If tests are skipped due to missing dependencies, install them using pip:
/root/miniconda3/envs/django_env/bin/python -m pip install tblib
  1. If you get database errors, ensure the test database can be created:
/root/miniconda3/envs/django_env/bin/python tests/runtests.py --settings=test_sqlite check
  1. For memory issues with large test suites, try running tests in sequence:
/root/miniconda3/envs/django_env/bin/python tests/runtests.py --parallel=1

Finding Test Names

To find specific test names for running individual tests:

  1. Run the module with verbosity 3:
/root/miniconda3/envs/django_env/bin/python tests/runtests.py --verbosity=3 --settings=test_sqlite module_name
  1. Look for the test names in the output
  2. Use the full path: module.class.test_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment