Bird
0
0

Consider the following pytest fixtures and test:

hard🚀 Application Q9 of 15
PyTest - Fixtures
Consider the following pytest fixtures and test:
import pytest

@pytest.fixture
def base_url():
    return "http://example.com"

@pytest.fixture
def api_endpoint(base_url):
    return f"{base_url}/api"

def test_api(api_endpoint):
    assert api_endpoint == "http://example.com/api"
What is the role of passing base_url as an argument to the api_endpoint fixture?
AIt is ignored; fixtures cannot accept arguments
BIt causes a runtime error because fixtures cannot depend on other fixtures
CIt allows <code>api_endpoint</code> to use the value returned by <code>base_url</code> fixture
DIt makes <code>api_endpoint</code> a test function instead of a fixture
Step-by-Step Solution
Solution:
  1. Step 1: Understand fixture dependency

    Fixtures can depend on other fixtures by accepting them as arguments. This allows reuse of fixture data.
  2. Step 2: Analyze the code behavior

    The 'api_endpoint' fixture uses the 'base_url' fixture's return value to build a URL string.
  3. Final Answer:

    It allows api_endpoint to use the value returned by base_url fixture -> Option C
  4. Quick Check:

    Fixture dependency = pass fixture as argument [OK]
Quick Trick: Fixtures can depend on other fixtures by accepting them as arguments [OK]
Common Mistakes:
MISTAKES
  • Thinking fixtures cannot depend on other fixtures
  • Assuming fixture arguments are ignored
  • Confusing fixtures with test functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes