Certainly! Here's a Python code implementation of an API test suite using pytest
for the test cases 1-10 you've listed:
import pytest
import requests
# Define the base URL of the API
BASE_URL = "https://example.com/api"
# Test Case 1: Positive Case - Valid Request
def test_valid_pet_creation():
# Send a valid JSON request body
payload = {
"name": "Fluffy",
"photoUrls": ["https://example.com/fluffy.jpg"]
}
response = requests.post(f"{BASE_URL}/pet", json=payload)
# Assertion
assert response.status_code == 200, "Expected status code 200 for a valid request."
assert "id" in response.json(), "The 'id' field is missing in the response."
# Clean up: Delete the created pet
pet_id = response.json()["id"]
delete_response = requests.delete(f"{BASE_URL}/pet/{pet_id}")
assert delete_response.status_code == 200, "Failed to delete the created pet."
# Test Case 2: Missing Required Field
def test_missing_required_field():
# Send a JSON request body with a missing required field
payload = {
"photoUrls": ["https://example.com/fluffy.jpg"]
}
response = requests.post(f"{BASE_URL}/pet", json=payload)
# Assertion
assert response.status_code == 400, "Expected status code 400 for missing required field."
assert "error" in response.json(), "Error message is missing in the response."
# Test Case 3: Invalid Input Data
def test_invalid_input_data():
# Send a JSON request body with invalid data
payload = {
"name": "Fluffy",
"photoUrls": "https://example.com/fluffy.jpg" # Invalid data type
}
response = requests.post(f"{BASE_URL}/pet", json=payload)
# Assertion
assert response.status_code == 400, "Expected status code 400 for invalid input data."
assert "error" in response.json(), "Error message is missing in the response."
# Test Case 4: Duplicate Pet ID
def test_duplicate_pet_id():
# Create a pet with a specific ID
payload = {
"id": 123,
"name": "Fluffy",
"photoUrls": ["https://example.com/fluffy.jpg"]
}
requests.post(f"{BASE_URL}/pet", json=payload)
# Attempt to create another pet with the same ID
duplicate_payload = {
"id": 123,
"name": "Duplicate Fluffy",
"photoUrls": ["https://example.com/duplicate-fluffy.jpg"]
}
response = requests.post(f"{BASE_URL}/pet", json=duplicate_payload)
# Clean up: Delete the created pet
delete_response = requests.delete(f"{BASE_URL}/pet/123")
assert delete_response.status_code == 200, "Failed to delete the created pet."
# Assertion
assert response.status_code == 409, "Expected status code 409 for duplicate pet ID."
assert "error" in response.json(), "Error message is missing in the response."
...