Cant raise error in a async function test

1k views Asked by At

Im using pytest-asyncio in a project that I'm currently working on.

In this project Im implementing the repository pattern and for tests I code a simple "In memory repository" (ie: dict with pk on keys and entities on values ). This repository is a class with async methods and have the following method:

    async def update(self, entity: IEntity) -> IEntity:
        try:
            if entity.id not in self._storage:
                raise KeyError()
            self._storage[entity.id] = entity
        except KeyError:
            raise EntityNotFound(
                f'Cant found {self._entity_type} with id: {entity.id}',
                _id=entity.id,
            )

        return entity

And I have the following test:

    @pytest.mak.asyncio
    async def test_delete_nonexistent_sale(self):
        with pytest.raises(EntityNotFound) as e:
            await self.service.handle({
                'sale_id': '93939393939393', 'salesman_id': self.salesman.id,
            })
            assert 1 == 2
            # Ignore this assert for now, Youll understand soon

where service.handle is another async function that has a await repository.update(pk) on the first line and has not try/catch inside.

The problem is that this pass ( that obviously should fail ) passes even with the assert 1==2. For some reason I cant even use pdb/ipdb.set_trace() after the repository call.

Pytest show me this warning:

purchase_system/tests/test_domain/test_services/test_delete_sale.py::TestDeleteSale::test_delete_nonexistent_sale
  /home/tamer.cuba/Documents/purchase-system/purchase_system/tests/test_domain/test_services/test_delete_sale.py:102: RuntimeWarning: coroutine 'DeleteSaleService.handle' was never awaited
    self.service.handle(

-- Docs: https://docs.pytest.org/en/stable/warnings.html

How can I propagate de exceptions in tests using pytest-asyncio ?

0

There are 0 answers