coveragepy
coveragepy copied to clipboard
pragma: must not run, for xfail style coverage
My usecase is a function that can never be called - It's only checked for callable-ness:
def dummy_view():
raise Exception("Cannot be called") # pragma: not covered
I'd like coverage to fail the build if that line is covered.
see also this SA question: https://stackoverflow.com/questions/47078737/is-there-an-equivalent-of-xfail-for-coverage
I don't understand why you need coverage to check this: if the line is executed, it will raise an exception, and your test will fail.
@nedbat it might not fail if someone accidentally catches the exception
I also do a lot of work in projects where exceptions go missing, or coroutines must be aborted at a particular step.
eg https://github.com/agronholm/anyio/blob/5ed3270c8d12cffc4cd3349d9ff32bc32451ae65/tests/test_taskgroups.py#L165-L172
I'd like to be able to use:
async def test_start_cancelled() -> None:
started = finished = False
async def taskfunc(*, task_status: TaskStatus) -> None:
nonlocal started, finished
started = True
await sleep(2)
finished = True # pragma: must not run
async with create_task_group() as tg:
tg.cancel_scope.cancel()
await tg.start(taskfunc)
assert started
assert not finished
@nedbat what would be acceptable for this feature? To have a new line on the report?
@Kludex I'm still trying to understand how people would use this. It still seems to me that raising an exception from the line that must not run would be the easiest way to get what's needed here.
This is for places where an exception may not be collected, eg in an aborted coroutine in a background task