How to Design Practical Login API Test Cases

This article presents several Python‑based login API test cases—including normal login, parameterized login, handling missing endpoints, and chaining an order request—along with step‑by‑step code examples and best‑practice tips for writing reliable, maintainable tests.

Woodpecker Software Testing
Woodpecker Software Testing
Woodpecker Software Testing
How to Design Practical Login API Test Cases

The article provides a collection of possible login‑interface test cases written in Python using the requests library.

1. Test normal login

# Request URL
url = "https://example.com/api/login"
# Request parameters
username = "testuser"
password = "testpassword"
# Send request and get login info
response = requests.post(url, data={
    "username": username,
    "password": password
})
# Parse response data
if response.status_code == 200:
    # Extract login information
    login_info = response.json()
    # Determine if login succeeded
    if login_info["success"] == True:
        print("登录成功")
    else:
        print("登录失败")
else:
    print("请求失败,状态码:", response.status_code)

2. Test login with parameters

# Request URL
url = "https://example.com/api/login"
# Request parameters
username = "testuser"
password = "testpassword"
# Send request and get login info
response = requests.post(url, data={
    "username": username,
    "password": password
})
# Parse response data
if response.status_code == 200:
    login_info = response.json()
    if login_info["success"] == True:
        print("登录成功")
    else:
        print("登录失败")
else:
    print("请求失败,状态码:", response.status_code)

3. Test a non‑existent login endpoint

# Request URL
url = "https://example.com/api/login"
# Request parameters
username = "testuser"
password = "testpassword"
# Send request and get login info
response = requests.post(url, data={
    "username": username,
    "password": password
})
# Parse response data
if response.status_code == 200:
    login_info = response.json()
    if login_info["success"] == True:
        print("登录成功")
    else:
        print("登录失败")
else:
    print("请求失败,状态码:", response.status_code)

4. Test sending an order after login and retrieve order information

# Request URL
url = "https://example.com/api/login"
# Request parameters
username = "testuser"
password = "testpassword"
# Send request and get login info
response = requests.post(url, data={
    "username": username,
    "password": password
})
# Parse response data
if response.status_code == 200:
    login_info = response.json()
    if login_info["success"] == True:
        print("登录成功")
        # Send order and get order info
        response = requests.post(login_info["order_url"], data={
            "order_id": login_info["order_id"],
            "out_trade_no": login_info["order_no"],
            "total_amount": login_info["total_price"],
            "remark": "购票订单"
        })
        # Parse order information
        order_info = response.json()
        print("订单状态:", order_info["status"])
        print("订单内容:", order_info["content"])
    else:
        print("登录失败")
else:
    print("请求失败,状态码:", response.status_code)

When writing test cases, the article advises to keep them as close as possible to real user scenarios, ensure coverage of all major functions and scenarios, follow a step‑by‑step execution principle, include detailed error messages for quick debugging, and design tests for repeatability and maintainability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAPI testingrequeststest caseslogin API
Woodpecker Software Testing
Written by

Woodpecker Software Testing

The Woodpecker Software Testing public account shares software testing knowledge, connects testing enthusiasts, founded by Gu Xiang, website: www.3testing.com. Author of five books, including "Mastering JMeter Through Case Studies".

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.