97

After tests execution is finished using Django's manage.py test command only number of passed tests is printed to the console.

(virtualenv) G:\Project\>python manage.py test
Creating test database for alias 'default'...
True
..
----------------------------------------------------------------------
Ran 2 tests in 0.017s

OK
Destroying test database for alias 'default'...

Is there any way to see:

  1. which tests were actually executed
  2. from what module
  3. in what order

I haven't found any solution in the doc.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162

2 Answers2

164

You can pass -v 2 to the test command:

python manage.py test -v 2

After running this command you'll get something like this (I'm using django 2, feel free to ignore migrations/database stuff):

Creating test database for alias 'default' ('file:memorydb_default?mode=memory&cache=shared')...
Operations to perform:
  Synchronize unmigrated apps: messages, staticfiles
  Apply all migrations: admin, auth, contenttypes, sessions
Synchronizing apps without migrations:
  Creating tables...
   Running deferred SQL...
Running migrations:
  Applying contenttypes.0001_initial... OK
  ...
  Applying sessions.0001_initial... OK
System check identified no issues (0 silenced).
test_equal_hard (polls.tests.TestHard) ... ok      <--------+
test_equal_simple (polls.tests.TestSimple) ... ok  <--------+
                                                            |
                                                            |
           That's your tests!  >----------------------------+

By the way, v stands for verbosity (You can also use --verbosity=2):

python manage.py test --verbosity=2

Here's the excerpt from the python manage.py test --help:

-v {0,1,2,3}, --verbosity {0,1,2,3}

Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Nigel Tufnel
  • 11,146
  • 4
  • 35
  • 31
  • I tried all verbosity levels but cannot find one which shows the unit tests without the migrations/database output. Is this really not possible without 3rd party libraries? – Addison Klinke Apr 12 '21 at 03:36
  • For hiding migration logs you could do something like `python manage.py test -v 2 | grep test*` – igorkf Jul 07 '21 at 14:57
24

Nigel's answer is great and definitely the lowest barrier to entry option. However, you can get even better feedback with django_nose (and it's not that difficult to setup ;).

The below is from: BDD with Python

First: install some requirements:

pip install nose pinocchio django_nose

Then add the following to settings.py

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['--with-spec', '--spec-color']

Then run your tests as per normal:

python manage.py test

Output should look something like this:

enter image description here

Note: The comments under your tests can be used to give even better output than just the name.

e.g.:

def test_something(self):
    """Something should happen"""
    ...

Will output "Something should happen" when running the test.

For extra points: You can also generate / output your code coverage:

pip install coverage

Add the following to your NOSE_ARGS in settings.py: '--with-coverage', '--cover-html', '--cover-package=.', '--cover-html-dir=reports/cover'

e.g.:

NOSE_ARGS = ['--with-spec', '--spec-color', 
         '--with-coverage', '--cover-html', 
         '--cover-package=.', '--cover-html-dir=reports/cover']

Then you'll get a nice code-coverage summary when you run python manage.py test as well as a neat html report in reports/cover

toast38coza
  • 8,650
  • 3
  • 39
  • 32
  • Hi @toast38coza. That's pretty neat. I'm new to Python and it's nice to see well formatted test output. I was looking for documentation for NOSE_ARGS, but none of what I found shows --With-spec and similar. Could you point me to that please? I'm basically trying to prevent nose from repeating "Similar to TransactionTestCase, but use `transaction.atomic()` to achieve test isolation.........nTestCase might be necessary (e.g. testing some transactional behavior)." – Macario Tala Feb 21 '20 at 20:41
  • @MacarioTala the --with-spec argument comes from the `pinocchio` plugin (you can pip install that). See where it says: "first install some requirements" above. – toast38coza Feb 21 '20 at 21:19
  • Yea. I've got that installed. Let me try to find documentation on pinocchio then. Thanks! – Macario Tala Feb 21 '20 at 21:25
  • 1
    In the interest of sharing, I swapped out pinocchio as it was a little TOO verbose, and instead of forking it, I found this: http://gfxmonk.net/dist/0install/rednose.xml , you might like it too. – Macario Tala Feb 21 '20 at 22:10