14

I'm attempting to set up a new django project, and I've configured TEST_RUNNER in settings.py to be django_nose.NoseTestSuiteRunner.

I chose this test runner because it seems to be the only one I can find that has the following features:

  • writes xunit xml test report
  • captures logging/stdout and only displays for failed tests.

However I've heard that nose is unmaintained and I'm having a hard time finding a suitable replacement. The standard test runner doesn't capture logging nor writes xunit as far as I'm able to tell (would love to be proven wrong!)

I run tests like so:

python -m coverage run manage.py test --noinput
python -m coverage report --include="app/*" --show-missing --fail-under=100
python -m coverage xml --include="app/*" -o ./reports/coverage.xml

With this in settings.py:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

And this setup.cfg:

[nosetests]
verbosity=0
with-xunit=1
xunit-file=./reports/xunit.xml
logging-clear-handlers=1

The last two lines are the real juicy bits I can't seem to find in other test runners. nose captures the logging and clears other logging handlers (eg, the handler that dumps on stdout) so the test runs output is much cleaner (you only see logging for tests that failed).

In other non-django projects I typically use nose2 but django-nose2 project appears to be 6 years old and lacking python3 support??

Please let me know which test runner is the "recommended" one (eg, most popular) with django support, thanks.

robru
  • 2,313
  • 2
  • 29
  • 38

3 Answers3

12

I have had success with unittest-xml-reporting:

TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'

https://github.com/xmlrunner/unittest-xml-reporting#django-support

The output directory can be configured with the TEST_OUTPUT_DIR setting.

Zags
  • 37,389
  • 14
  • 105
  • 140
Juuso Ohtonen
  • 8,826
  • 9
  • 65
  • 98
5

You may still use nose runner:

INSTALLED_APPS += ['django_nose']
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = [
    '--with-xunit',
    '--xunit-file=nosetests.xml',
    '--with-coverage',
    '--cover-erase',
    '--cover-xml',
    '--cover-xml-file=nosecover.xml',
]
awka
  • 51
  • 1
  • 2
2

So pytest produces some very nice test output. I've unset TEST_RUNNER in settings.py and changed my test script to:

python -m coverage run -m pytest --junitxml=./reports/junit.xml
python -m coverage report --include="app/*" --show-missing --fail-under=100
python -m coverage xml --include="app/*" -o ./reports/coverage.xml

This works, and captures ALL logging output (nose was a little buggy and let one or two logging statements slip through, very strange behavior).

The only thing is that I'm a django novice so I don't know if there are any bad side-effects of not using manage.py test for testing django. Any guidance is appreciated, thanks!

robru
  • 2,313
  • 2
  • 29
  • 38
  • 1
    I tried to follow your instructions and it doesn't work - I think you might have something else that you did to actually make this all work. – Ash Berlin-Taylor Nov 06 '17 at 14:58