coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

pragma: must not run, for xfail style coverage

Open graingert opened this issue 5 years ago • 6 comments

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

graingert avatar Sep 20 '18 21:09 graingert

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 avatar Sep 20 '18 23:09 nedbat

@nedbat it might not fail if someone accidentally catches the exception

graingert avatar Sep 20 '18 23:09 graingert

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

graingert avatar Jul 25 '21 07:07 graingert

@nedbat what would be acceptable for this feature? To have a new line on the report?

Kludex avatar Jul 04 '22 20:07 Kludex

@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.

nedbat avatar Jul 04 '22 23:07 nedbat

This is for places where an exception may not be collected, eg in an aborted coroutine in a background task

graingert avatar Jul 04 '22 23:07 graingert