0
0
PyTesttesting~20 mins

Async fixtures (pytest-asyncio) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Fixture Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of async fixture usage in pytest
What is the output of running this pytest test with an async fixture?
PyTest
import pytest
import asyncio

@pytest.fixture
async def async_resource():
    await asyncio.sleep(0.1)
    return 'resource_ready'

@pytest.mark.asyncio
async def test_async_fixture(async_resource):
    assert async_resource == 'resource_ready'
    print(async_resource)
ATest fails with AssertionError
BTest passes and prints 'resource_ready'
CTest raises RuntimeError due to event loop
DTest is skipped due to missing marker
Attempts:
2 left
💡 Hint
Remember that pytest-asyncio supports async fixtures and test functions with the @pytest.mark.asyncio decorator.
assertion
intermediate
1:30remaining
Correct assertion for async fixture return value
Given an async fixture that returns a dictionary {'status': 'ok', 'code': 200}, which assertion correctly verifies the 'code' key in a pytest async test?
PyTest
import pytest

@pytest.fixture
async def api_response():
    return {'status': 'ok', 'code': 200}

@pytest.mark.asyncio
async def test_api_code(api_response):
    # Which assertion is correct here?
    pass
Aassert api_response['code'] == 200
Bassert api_response['code'] != 200
Cassert api_response.get('code') is '200'
Dassert api_response.code == 200
Attempts:
2 left
💡 Hint
Dictionaries use bracket notation to access keys, and '==' compares values correctly.
🔧 Debug
advanced
2:00remaining
Identify the cause of RuntimeError in async fixture test
Why does this pytest async test raise RuntimeError: This event loop is already running?
PyTest
import pytest
import asyncio

@pytest.fixture
async def async_data():
    await asyncio.sleep(0.1)
    return 42

def test_sync_using_async_fixture(async_data):
    assert async_data == 42
AThe fixture does not return a coroutine
BThe fixture is missing @pytest.mark.asyncio decorator
CThe test function is not async and tries to use an async fixture synchronously
DThe test function is missing a call to asyncio.run()
Attempts:
2 left
💡 Hint
Async fixtures must be used in async test functions or with proper event loop management.
🧠 Conceptual
advanced
1:30remaining
Purpose of async fixtures in pytest-asyncio
What is the main benefit of using async fixtures in pytest-asyncio?
AThey allow setup and teardown code to run asynchronously, improving test performance when dealing with async resources
BThey automatically convert synchronous fixtures to async functions
CThey enable running tests without an event loop
DThey replace the need for mocking in tests
Attempts:
2 left
💡 Hint
Think about why async code needs async setup and cleanup.
framework
expert
2:30remaining
Correct pytest-asyncio fixture and test integration
Which option shows the correct way to define and use an async fixture with pytest-asyncio to test an async function that returns 'done' after a delay?
PyTest
import pytest
import asyncio

async def async_func():
    await asyncio.sleep(0.05)
    return 'done'

# Choose the correct fixture and test code
A
@pytest.fixture
def fixture_func():
    return asyncio.run(async_func())

@pytest.mark.asyncio
def test_func(fixture_func):
    assert fixture_func == 'done'
B
@pytest.fixture
async def fixture_func():
    return async_func()

def test_func(fixture_func):
    assert fixture_func == 'done'
C
@pytest.fixture
async def fixture_func():
    result = async_func()
    return result

@pytest.mark.asyncio
async def test_func(fixture_func):
    assert await fixture_func == 'done'
D
@pytest.fixture
async def fixture_func():
    result = await async_func()
    return result

@pytest.mark.asyncio
async def test_func(fixture_func):
    assert fixture_func == 'done'
Attempts:
2 left
💡 Hint
Async fixtures must await async calls and tests must be async with the marker.