0
0
PyTesttesting~10 mins

Async fixtures (pytest-asyncio) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an async fixture using pytest-asyncio.

PyTest
import pytest

@pytest.fixture
async def sample_fixture():
    await [1]()
    return 42
Drag options to blanks, or click blank then click option'
Atime.sleep(1)
Basyncio.sleep(0)
Cprint('hello')
Dopen('file.txt')
Attempts:
3 left
💡 Hint
Common Mistakes
Using a synchronous function like time.sleep inside an async fixture causes errors.
Not using await before the async call.
2fill in blank
medium

Complete the code to mark the test function as async to use the async fixture.

PyTest
import pytest

@pytest.mark.asyncio
async def test_example([1]):
    result = await sample_fixture
    assert result == 42
Drag options to blanks, or click blank then click option'
Asample
Bfixture_sample
Csample_fixture
Dfixture
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong parameter name that does not match the fixture.
Forgetting to mark the test as async with async def.
3fill in blank
hard

Fix the error in the async fixture by adding the missing decorator.

PyTest
import pytest

async def [1]():
    await asyncio.sleep(0.1)
    return 'done'
Drag options to blanks, or click blank then click option'
Apytest.fixture
Bfixture
Casync_fixture
Dpytest.mark.asyncio
Attempts:
3 left
💡 Hint
Common Mistakes
Using @pytest.mark.asyncio instead of @pytest.fixture for fixtures.
Not adding any decorator causes pytest to ignore the fixture.
4fill in blank
hard

Fill both blanks to create an async fixture that yields a resource and cleans up after the test.

PyTest
import pytest

@pytest.fixture
async def resource():
    res = await setup_resource()
    yield [1]
    await [2](res)
Drag options to blanks, or click blank then click option'
Ares
Bcleanup_resource
Cres.cleanup
Dclose_resource
Attempts:
3 left
💡 Hint
Common Mistakes
Yielding the wrong variable name.
Calling a cleanup method that does not exist or forgetting to await it.
5fill in blank
hard

Fill all three blanks to write a test using an async fixture and assert the awaited result.

PyTest
import pytest

@pytest.mark.asyncio
async def test_resource([1]):
    result = await [2]
    assert result == [3]
Drag options to blanks, or click blank then click option'
Aresource
C'ready'
D'done'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong fixture name in the test parameter.
Asserting the wrong expected value.