16

Is there an easy way to get a list of all tests in a Django project without running the tests themselves? I was hoping for something like ./manage.py test --list.

Hakan B.
  • 2,319
  • 23
  • 29
  • 3
    besides `grep -R "^\w*def test_.*\(.*\)\:$" --include "*.py"`? – Thomas May 03 '13 at 06:57
  • 2
    Running that on OS X 10.8.3 returned `grep: warning: recursive search of stdin` and the process hung indefinitely without returning any results. Modifying the command to `grep -R "def test_" --include "*.py" .` worked for me. Thanks for the clever solution. – Hakan B. May 03 '13 at 07:25
  • I'm not using `nose`, so this grep-based approach worked great for me! – Wilhelm Klopp Jun 28 '21 at 10:29
  • Unfortunately the `grep` method doesn't work when test functions are generated dynamically. – Neil C. Obremski Nov 04 '21 at 21:55

2 Answers2

17

In my opinion, the more correct way is to use the actual tool for running tests. E.g. in case of nose:

./manage.py test <app> --verbosity 2 --collect-only

FYI, py.test also has --collectonly option to print tests instead of executing.

Also see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    I don't see `nose` options when running either `manage.py --help` or `mange.py test --help`, unfortunately. It's strange because I can see my tests are definitely being run via `nose`. I tried reinstalling `django-nose` but no luck. It must be something on my end. Thanks for your help. – Hakan B. May 21 '13 at 20:23
  • 2
    You are welcome. Please try to move `django_nose` to the end of `INSTALLED_APPS`. – alecxe May 21 '13 at 20:26
  • That did it, thanks! I now see the `nose` command-line options and your original answer to the question works as well. – Hakan B. May 21 '13 at 20:31
0

In my case, I'm running the command with Docker, but just ignore the Docker command and get the command inside the quotes.

django.test | unittest

sudo docker compose run --rm app sh -c "python manage.py test --verbosity 2 --force-color"
(venv) ➜  recipe-app-api git:(main) ✗ sudo docker compose run --rm app sh -c "python manage.py test --verbosity 2 --force-color"
[+] Running 1/0
 ⠿ Container recipe-app-api-db-1  Running                                                                                                         0.0s
Skipping setup of unused database(s): default.
System check identified no issues (0 silenced).
test_add_numbers (app.tests.CalcTests)
Test adding number together. ... ok
test_subtract_numbers (app.tests.CalcTests)
Test subtracting numbers. ... ok
test_wait_for_db_delay (core.tests.test_commands.CommandTests)
Test waiting for database when  getting OperationalError. ... Waiting for database...
Database unvailable, waiting 1 second...
Database unvailable, waiting 1 second...
Database unvailable, waiting 1 second...
Database unvailable, waiting 1 second...
Database unvailable, waiting 1 second...
Database available!
ok
test_wait_for_db_ready (core.tests.test_commands.CommandTests)
Test waiting for database if database ready. ... Waiting for database...
Database available!
ok

----------------------------------------------------------------------
Ran 4 tests in 0.007s

OK

You can pass the verbosity or just -v parameter. The -v parameter has more 4 options to display the test in the screen.

For more information regarding the options:

usage: manage.py test [-h] [--noinput] [--failfast] [--testrunner TESTRUNNER] [-t TOP_LEVEL] [-p PATTERN] [--keepdb] [-r] [--debug-mode] [-d]
                      [--parallel [N]] [--tag TAGS] [--exclude-tag EXCLUDE_TAGS] [--pdb] [-b] [--no-faulthandler] [--timing] [-k TEST_NAME_PATTERNS]
                      [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
                      [test_label ...]
  • The question specifies that the tests should only be counted (not run). The output from your example indicates the tests are being run, making this answer unsuitable. – CoatedMoose Jul 12 '22 at 21:24