pytest-check icon indicating copy to clipboard operation
pytest-check copied to clipboard

Is early error checking possible?

Open skhomuti opened this issue 3 years ago • 4 comments

Is there now a surefire way to check for errors even before the test ends?

I can do something like this, but it seems that this is not quite the intended scenario for using pytest-check

import pytest_check as check
from pytest_check.check_methods import get_failures


def test_some():
    check.equal(1, 2, msg="Numbers are not equal")
    check.equal(2, 1, msg="Numbers are not equal")
    assert not get_failures()
    raise NotImplementedError

skhomuti avatar Nov 01 '21 08:11 skhomuti

Failing early isn't something I considered before, but not a bad idea. Maybe something like an assert_no_failures() function. Would that work for your use cases?

okken avatar Nov 30 '21 14:11 okken

exactly what is needed!

skhomuti avatar Dec 01 '21 06:12 skhomuti

This is already doable in 2 ways, see below. I sometimes use the first (get_failures()). Haven't used th second one as I just use assert instead.

  1. Call get_failures()
    import pytest_check as check
    from pytest_check.check_methods import get_failures
    
    def test_something():
        do_something()
        check.equal(...)
        check.equal(...)
        assert not get_failures()  # Test case ends here if any `check` above failed
        check.equal(...)
    
  2. Doset_stop_on_fail(True) before the checks.

joaonc avatar Jan 24 '22 02:01 joaonc

geeezz I now read the whole first comment where it already states the use of get_failures(), sorry.

That said, I think its use quite reasonable. Bummer that we need an extra import though.

joaonc avatar Jan 24 '22 02:01 joaonc

check.any_failures() is now implemented, so a workable workaround is:

assert not check.any_failures()

okken avatar Dec 09 '22 18:12 okken