Add @example(...).xfail(...)
to check inputs which are expected to fail
#3530
Labels
new-feature
entirely novel capabilities or strategies
A classic error when testing is to write a test function that can never fail, even on inputs that aren't allowed or manually provided. @rsokl mentioned a nice design pattern for
@pytest.mark.parametrize()
of including an xfailed param to check that the test can fail; and after some discussion we agreed that this would be nice to have in Hypothesis too.So: following #3516, I'd like to add an
@example(...).xfail(condition: bool = True, *, reason: str = "", raises: type[BaseException] | tuple[...] = BaseException)
- matching the interface ofpytest.mark.xfail()
(omitting the subset that doesn't make sense here). Implementation-wise, this will returnself
ifcondition
is False, otherwise return an instance of a new XfailExample subclass which is known and handled by the execution logic incore.py
. If the exception raised is not an instance ofraises
and ourfailure_exceptions_to_catch()
it propogates as usual; if nothing is raised then we raise an error (usingpytest.xfail()
if available). Naturally we'll also support.via().xfail()
and.xfail().via()
😁The text was updated successfully, but these errors were encountered: