0
0
PyTesttesting~10 mins

API client testing in PyTest - Interactive Code Practice

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

Complete the code to import the pytest module for testing.

PyTest
import [1]
Drag options to blanks, or click blank then click option'
Apytest
Bunittest
Crequests
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong module like unittest or requests.
Forgetting to import pytest before writing tests.
2fill in blank
medium

Complete the code to send a GET request using the requests library.

PyTest
response = requests.[1]('https://api.example.com/data')
Drag options to blanks, or click blank then click option'
Aput
Bpost
Cget
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using post or put instead of get for retrieving data.
Misspelling the method name.
3fill in blank
hard

Fix the error in the assertion to check the response status code is 200.

PyTest
assert response.status_code [1] 200
Drag options to blanks, or click blank then click option'
A!=
B==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using != which checks for inequality.
Using > or < which are incorrect for exact match.
4fill in blank
hard

Fill both blanks to check if the JSON response contains a key 'name' and its value is not empty.

PyTest
data = response.json()
assert '[1]' in data and data['[2]'] != ''
Drag options to blanks, or click blank then click option'
Aname
Bid
Cemail
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys in the two blanks.
Checking keys that do not exist in the response.
5fill in blank
hard

Fill all three blanks to create a test function that sends a GET request, checks status code 200, and asserts the response contains a 'success' key with value True.

PyTest
def test_api_response():
    response = requests.[1]('https://api.example.com/status')
    assert response.status_code [2] 200
    assert response.json().get('[3]') is True
Drag options to blanks, or click blank then click option'
Aget
B==
Csuccess
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for the request.
Using wrong comparison operators in assertions.
Checking for a wrong key in the JSON response.