gleb-sevruk / pycrunch-engine

NCrunch inspired tool for continuous testing Python
https://pycrunch.com
Other
57 stars 8 forks source link

run async tests #52

Closed hamedsh closed 3 years ago

hamedsh commented 3 years ago

consider this function and test

# main.py
import asyncio

async def calculator(a: int, b: int) -> int:
    await asyncio.sleep(1)
    return a + b
# test_main.py
import pytest

from main import calculator

@pytest.mark.asyncio
async def test_calculator_sum():
    expected = 5
    subject = await calculator(2, 3)
    assert subject == expected

this test will fail with the following error:

self = <_UnixSelectorEventLoop running=False closed=False debug=False>

    def _check_running(self):
        if self.is_running():
            raise RuntimeError('This event loop is already running')
        if events._get_running_loop() is not None:
>           raise RuntimeError(
                'Cannot run the event loop while another loop is running')
E           RuntimeError: Cannot run the event loop while another loop is running

/usr/lib/python3.8/asyncio/base_events.py:554: RuntimeError
hamedsh commented 3 years ago

Solution:

from main import calculator

nest_asyncio.apply()

@pytest.mark.asyncio async def test_calculator_sum(): expected = 5 subject = await calculator(2, 3) assert subject == expected


- add this line to engine section in `.pycrunc-config.yaml`: `load-pytest-plugins: true`