GDorn
GDorn

Reputation: 8811

How to disable Django's Test Discovery?

This is an unusual situation - most Django users want Django's test runner to find all of their tests.

I'm working on a python library with several test suites that are run with different configurations, so I don't want the discovery to find and run tests from the wrong configuration. How do I disable discovery entirely and rely on the pre-1.6 behavior of only running the tests for apps explicitly declared in INSTALLED_APPS?

My library structure:

library/    # django app used by others
tests/      # custom test suites here
  core/     # tests of core functionality
  custom/   # tests of a custom feature requiring separate config
  contrib/  # tests for assorted contrib features, also requiring separate config
  manage_core.py    # separate manage.py files for each "project"
  manage_custom.py  # these specify settings file to use.
  manage_contrib.py
  settings.py         # base settings for all tests
  settings_core.py    # settings for 'core' tests including unique INSTALLED_APPS
  settings_custom.py  # settings for 'custom' tests; different INSTALLED_APPS
  settings_contrib.py # settings for 'contrib' tests; different INSTALLED_APPS

The problem is that this command, which should only run tests for the 'contrib' test suite, is also finding and running tests for 'core': ./manage_contrib.py test contrib.tests

Upvotes: 1

Views: 421

Answers (2)

GDorn
GDorn

Reputation: 8811

It's missing from the Django docs, but the command-line has an option, found via ./manage.py help test:

  -t TOP_LEVEL, --top-level-directory TOP_LEVEL
                    Top level of project for unittest discovery.

Confusingly, specifying the module to test doesn't appear to prevent test discovery, but specifying a sub-directory does, like this:

./manage_contrib.py test contrib.tests -t ./contrib/

That appears to prevent the discovery of tests located outside of contrib.

Upvotes: 1

Robert Townley
Robert Townley

Reputation: 3574

Hmm I'm unfortunately not aware of a settings parameter that might let you tell unittest to only run from individual apps (a-la "settings.TEST_DIRECTORIES=settings.INSTALLED_APPS") but if you're able to give your tests a unique naming convention, you could use the --pattern= option when running the test suite.

For example, if you have

/myapp/tests/test_a_models.py
/myapp/tests/test_b_models.py

You could only run a with ./manage.py test --pattern='*_a_*' and then run b with ./manage.py test --pattern='*_b_*'

Definitely not ideal, but might get the job done depending on how much flexibility you have with the test naming conventions in your own app.

Upvotes: 0

Related Questions