[Python] How to use pytest to assert NO Warning is raised

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    I want to ensure that no warning at all is raised in one assertion.

    Could not find any explicit answer in pytest documentation about warnings. (dead link, the 3.2.* doc is not available anymore).

    I've tried this, thinking maybe None would mean "nothing":

    def test_AttrStr_parse_warnings():
    """Check _AttrStr.parse() raises proper warnings in proper cases."""
    with pytest.warns(None):
    _AttrStr('').parse()


    but this assertion is also always correct, for instance, the test does not fail, even if a warning is actually raised:

    def test_AttrStr_parse_warnings():
    """Check _AttrStr.parse() raises proper warnings in proper cases."""
    with pytest.warns(None):
    _AttrStr('').parse()
    warnings.warn('any message')

    Continue reading...

Compartilhe esta Página