Skip to content

howto pytest warnings

Ludovico Bianchi edited this page Aug 23, 2024 · 2 revisions

How to handle warnings in pytest

Background

  • Soon ™️, the IDAES test suite will start enforcing a "no warnings" policy: unexpected warnings emitted while a test is running will cause the test to fail
  • If there are warnings that are expected to be raised/emitted (either by IDAES code or third-party code) during a test, that test must be modified to properly account for these expected warnings

Guiding principles

  • Be as specific as possible

Option A: pytest.mark.filterwarnings

This decorates a test function, class, or module with a pytest marker that uses Python's built-in warnings filter functionality.

Example 1: ignoring all warnings (BAD)

@pytest.mark.unit
@pytest.mark.filterwarnings("ignore")
def test_my_code():
    res = np.log(-1)

Example 2: ignoring only warnings of a specific type (better)

Much like exceptions, warnings can have specific types. This can be specified as the third segment of the filterwarnings string format:

@pytest.mark.unit
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test_my_code():
    res = np.log(-1)

Example 3: ignoring warnings matching a specific message (best)

The second section of the filterwarnings format can be used to specify a regular expression (regex) pattern:

@pytest.mark.unit
@pytest.mark.filterwarnings("ignore:invalid value encountered in log::RuntimeWarning")
def test_my_code():
    res = np.log(-1)

Multiple variations and other more complex filters are supported with regex group constructs:

@pytest.mark.unit
@pytest.mark.filterwarnings("ignore:invalid value encountered in (log|sqrt)::RuntimeWarning")
def test_my_code():
    x = np.log(-1)
    y = np.sqrt(-1)

More examples

Examples in the IDAES codebase: https://github.com/search?q=repo%3AIDAES%2Fidaes-pse+pytest.mark.filterwarnings&type=code

See also

Clone this wiki locally