Challenge - 5 Problems
Async Fixture Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that pytest-asyncio supports async fixtures and test functions with the @pytest.mark.asyncio decorator.
✗ Incorrect
The async fixture 'async_resource' awaits a short sleep and returns a string. The test uses this fixture and asserts the returned value. Since the fixture and test are properly async and marked, the test passes and prints the string.
❓ assertion
intermediate1: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
Attempts:
2 left
💡 Hint
Dictionaries use bracket notation to access keys, and '==' compares values correctly.
✗ Incorrect
Option A correctly accesses the dictionary key 'code' and compares it to the integer 200. Option A is invalid because dictionaries do not support attribute access. Option A compares with 'is' and a string, which is incorrect. Option A asserts the opposite of the expected value.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Async fixtures must be used in async test functions or with proper event loop management.
✗ Incorrect
The test function is synchronous but uses an async fixture. Pytest tries to run the async fixture in the current event loop, which is already running, causing the RuntimeError. Making the test async or using pytest-asyncio marker fixes this.
🧠 Conceptual
advanced1:30remaining
Purpose of async fixtures in pytest-asyncio
What is the main benefit of using async fixtures in pytest-asyncio?
Attempts:
2 left
💡 Hint
Think about why async code needs async setup and cleanup.
✗ Incorrect
Async fixtures let you write setup and teardown code that uses await, which is useful for async resources like network calls or databases. This can make tests faster and more realistic. They do not convert sync fixtures or remove the need for mocking.
❓ framework
expert2: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
Attempts:
2 left
💡 Hint
Async fixtures must await async calls and tests must be async with the marker.
✗ Incorrect
Option D correctly awaits async_func inside the async fixture and returns the awaited result. The test is async and uses the fixture value directly. Option D returns a coroutine without awaiting, and test is sync, causing errors. Option D uses asyncio.run inside a sync fixture, which is discouraged and test is sync with marker mismatch. Option D returns coroutine without await in fixture and tries to await in test, which is invalid.