Skip to content

Instantly share code, notes, and snippets.

@sgrebnov
Last active October 23, 2023 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgrebnov/f86e47944a0d565373aeca090d5c2cff to your computer and use it in GitHub Desktop.
Save sgrebnov/f86e47944a0d565373aeca090d5c2cff to your computer and use it in GitHub Desktop.

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."

...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment