0
0
PyTesttesting~20 mins

API client testing in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Client Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this pytest API client test?

Consider this pytest test function that uses a mock API client to test a GET request response status code.

PyTest
import pytest
from unittest.mock import Mock

class APIClient:
    def get(self, url):
        # Simulate a real HTTP GET request
        return Mock(status_code=200)

def test_get_status_code():
    client = APIClient()
    response = client.get('/resource')
    assert response.status_code == 200
    print('Test passed')
AAttributeError
BAssertionError
CTest passed
DSyntaxError
Attempts:
2 left
💡 Hint

Look at the mocked response status_code and the assertion.

assertion
intermediate
1:30remaining
Which assertion correctly verifies JSON response content?

You receive this JSON response from an API client: {"user": "alice", "active": true}. Which pytest assertion correctly checks that the user is 'alice' and active is True?

Aassert response.json()['user'] == 'alice' and response.json()['active'] is True
Bassert response.json()['user'] = 'alice' and response.json()['active'] == True
Cassert response.json().user == 'alice' and response.json().active == True
Dassert response.json()['user'] == alice and response.json()['active'] == true
Attempts:
2 left
💡 Hint

Remember how to access dictionary keys and compare values in Python.

locator
advanced
2:00remaining
Which locator correctly mocks an API client method for a POST request?

You want to mock the post method of an API client instance client to return a response with status code 201. Which option correctly sets this up using unittest.mock?

Aclient.post = Mock(return_value=Mock(status_code=201))
Bclient.post.return_value.status_code = 201
CMock(client.post, return_value=Mock(status_code=201))
Dclient.post = Mock(status_code=201)
Attempts:
2 left
💡 Hint

Think about how to assign a mock method that returns a mock response.

🔧 Debug
advanced
2:00remaining
Why does this pytest API test fail with AttributeError?

Examine this test code snippet:

def test_api_call(client):
    response = client.get('/data')
    assert response.status == 200

The test fails with AttributeError: 'Mock' object has no attribute 'status'. Why?

AThe assertion syntax is incorrect.
BThe response object uses 'status_code' attribute, not 'status'.
CThe client.get method is not mocked properly.
DThe URL '/data' is invalid causing no response.
Attempts:
2 left
💡 Hint

Check the attribute names commonly used in HTTP response objects.

framework
expert
3:00remaining
Which pytest fixture setup ensures isolated API client tests?

You want to write pytest tests for an API client where each test gets a fresh client instance with a mocked get method returning status 200. Which fixture setup is best?

A
@pytest.fixture(scope='session')
def client():
    client = APIClient()
    client.get = Mock(return_value=Mock(status_code=200))
    return client
B
@pytest.fixture(scope='module')
def client():
    client = APIClient()
    client.get = Mock(return_value=Mock(status_code=200))
    return client
C
@pytest.fixture(autouse=True)
def client():
    client = APIClient()
    client.get = Mock(return_value=Mock(status_code=200))
    return client
D
@pytest.fixture
def client():
    client = APIClient()
    client.get = Mock(return_value=Mock(status_code=200))
    return client
Attempts:
2 left
💡 Hint

Consider fixture scope and test isolation.