10

this is an expansion of How do I find where a "Sorting because non-concatenation" warning is coming from?.

I'm still getting the same warning, in my pytest. I've looked at several questions here, and done:

import warnings
warnings.filterwarnings('error')

which is suggested in How do I catch a numpy warning like it's an exception (not just for testing)?

However, when I run pytest, it still gives me the error, but nothing actually errors...

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Brian Postow
  • 11,709
  • 17
  • 81
  • 125

3 Answers3

13

Try passing the -W flag when you run pytest, like this:

pytest -W error::RuntimeWarning

Specify the kind of warning you want to turn in to an error e.g. DeprecationWarning, FutureWarning, UserWarning.

Christian Long
  • 10,385
  • 6
  • 60
  • 58
  • 4
    How can I set this in a config file so I don't need to manually pass it on every pytest call? I don't see a longhand name for the `-W` option? – Johnny Metz May 22 '22 at 02:04
  • 3
    The same option can be set in the pytest.ini or pyproject.toml file using the `filterwarnings` ini option. [Link to docs](https://docs.pytest.org/en/latest/how-to/capture-warnings.html#controlling-warnings) with examples of the syntax for pytest.ini and pyproject.toml. – Christian Long May 24 '22 at 19:45
1

If you want to make every warning raise an error then just use

pytest -W error
Qback
  • 4,310
  • 3
  • 25
  • 38
0

Wanted to share another solution in hopes that it will help others as I spent way too long trying to solve this.

I specifically only wanted a single test to fail on a warning, not all of them. In my case an exception was being raised within a thread I wanted to test for and discovered the pytest.mark.filterwarnings decorator can be used for this purpose.

The traceback:

        raise SerialException(
    serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

        warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))

-- Docs: https://docs.pytest.org/en/stable/warnings.html

The decorator to catch it:

@pytest.mark.filterwarnings("error::pytest.PytestUnhandledThreadExceptionWarning")
Teejay Bruno
  • 1,716
  • 1
  • 4
  • 11