How to see which tests were run during Django's manage.py test command
Asked Answered
A

2

101

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.

Ava answered 1/2, 2014 at 16:9 Comment(0)
F
168

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

Fillet answered 1/2, 2014 at 16:16 Comment(2)
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?Lunette
For hiding migration logs you could do something like python manage.py test -v 2 | grep test*Hyksos
G
25

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

Gaucho answered 5/2, 2016 at 12:25 Comment(4)
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)."Homolographic
@MacarioTala the --with-spec argument comes from the pinocchio plugin (you can pip install that). See where it says: "first install some requirements" above.Gaucho
Yea. I've got that installed. Let me try to find documentation on pinocchio then. Thanks!Homolographic
In the interest of sharing, I swapped out pinocchio as it was a little TOO verbose, and instead of forking it, I found this: gfxmonk.net/dist/0install/rednose.xml , you might like it too.Homolographic

© 2022 - 2024 — McMap. All rights reserved.