0

I'm attempting to run Django unit tests using:

python -We -W ignore:DeprecationWarning -W ignore:UserWarning manage.py test

However, this doesn't successfully ignore these warning classes, and instead only respects the -We flag, raising all warnings to exceptions. This is not my desired behavior.

How can I run Django tests such that all warnings except DeprecationWarning and UserWarning are raised as exceptions when encountered in a test?

Use case: the project I am working on sees a lot of RuntimeWarnings being raised because of naive datetimes in the tests. I want to fix this, but to do so, I have to locate the problematic tests.

Adam
  • 3,668
  • 6
  • 30
  • 55

1 Answers1

2

The second argument to the filter is message, not module so your exclusions should be:

python -We -W ignore::DeprecationWarning -W ignore::UserWarning manage.py test

Note the additional colons and see example here: https://docs.python.org/3/library/warnings.html#default-warning-filter

You could also go the other way and only error on RuntimeWarning

python -W error::RuntimeWarning manage.py test

To solve the more general problem you're asking about however it will likely be quicker to print tracebacks with each of your warnings instead. See this SO post: https://stackoverflow.com/a/22376126/11847125

azundo
  • 5,902
  • 1
  • 14
  • 21