0

To run my Django's tests, I was using the manage.py file. But I decided to create my own file runtests.py.

As specified in the Django's doc, the manage.py file will execute all methods whose name is starting like "test" and whose class inherits TestCase !

My aim is to use this searching method to display all possible tests (not running them). So do you know where I could get this "searching method" ?

Thank you !

AntoineLB
  • 482
  • 3
  • 19
  • https://docs.djangoproject.com/en/1.9/topics/testing/overview/ says "You can also provide a path to a directory to discover tests below that directory" $ ./manage.py test animals/ – NeilG Jul 16 '19 at 03:35

1 Answers1

0

What you're looking for is called "test discovery", and you don't need to do it manually.

If you want to write a standalone script that runs your tests, you can simply invoke Django's test runner manually from Python code after configuring. The process for this is:

# First you need settings.
# You can do this by setting DJANGO_SETTINGS_MODULE:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'path.to.your.settings')

# Or you can do it manually:
SETTINGS_DICT = {
    # Put all the settings you'll need in here...
}
from django.conf import settings
settings.configure(**SETTINGS_DICT)

import django
django.setup()

import sys

# Now you can run tests:
from django.test.utils import get_runner
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=2, interactive=True)
failures = test_runner.run_tests(
    ['test_module_to_look_in']
)
sys.exit(bool(failures))
James Bennett
  • 10,903
  • 4
  • 35
  • 24
  • Thank you, but I would actually only display all test cases of my app.. – AntoineLB May 04 '18 at 09:25
  • Django's test discovery uses the Python unittest module's test discovery and loading code. You can read through how Django does it here: https://github.com/django/django/blob/02114f9c49a1cad3f7fe86fe36fcda18632ffb7e/django/test/runner.py#L396 – James Bennett May 04 '18 at 12:28