Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
API testing APIs APIs From Zero to Real-World API Testing & Automation Module 6 — API Testing

Positive vs Negative API Testing — With Real Examples

Reviewed & accurate
AI Summary

What You'll Learn

  • The difference between positive and negative testing
  • How to design effective negative tests
  • Common negative test scenarios for any API
  • How to write assertions for both

Why This Matters

Most bugs in production are caused by inputs the developer did not anticipate. Positive tests verify the API works when used correctly — but real users do unexpected things. They send missing fields, wrong types, huge values, special characters. Negative tests verify the API handles these gracefully instead of crashing or leaking data.

Simple Definitions

Positive testing = send valid input, expect success.
Negative testing = send invalid input, expect a controlled failure.

Both are mandatory. A test suite with only positive tests gives false confidence — the API works when nothing goes wrong, but you have no idea what happens when something does.

Real-World Analogy — Door Testing

Imagine testing a front door lock. Positive test: insert the correct key, verify the door opens. Negative tests: insert the wrong key, verify it does not open. Insert the key upside down. Insert a credit card. Try with no key at all. Try with a bent key.

Without negative tests, you only know the door opens for the right key. You don't know whether it also opens for a credit card.

Positive Test Example

Scenario: Create a user with valid data.

Request:
POST /users HTTP/1.1
Content-Type: application/json

{
  "name": "Anita Sharma",
  "email": "anita@example.com",
  "age": 28
}

Expected response:
HTTP/1.1 201 Created
Content-Type: application/json
Location: /users/123

{
  "id": 123,
  "name": "Anita Sharma",
  "email": "anita@example.com",
  "age": 28,
  "created_at": "2026-08-23T09:14:00Z"
}

Assertions:
status code == 201
response.body.id is a number
response.body.name == "Anita Sharma"
response.body.email == "anita@example.com"
response.body.age == 28
response.headers.Location is set
response.body.created_at is a valid ISO 8601 date

Negative Test Examples

Negative Test 1 — Missing required field

Scenario: Create a user without the required email field.

Request:
POST /users
Content-Type: application/json

{
  "name": "Anita Sharma",
  "age": 28
}

Expected response:
HTTP/1.1 422 Unprocessable Entity

{
  "error": {
    "code": "VALIDATION_FAILED",
    "message": "The 'email' field is required.",
    "details": [{"field": "email", "issue": "required"}]
  }
}

Assertions:
status code == 422
response.body.error.code == "VALIDATION_FAILED"
response.body.error.details contains field "email"
no user was created in the database

Negative Test 2 — Invalid email format

Scenario: Email field is not a valid email.

Request body: {"name": "Anita", "email": "not-an-email", "age": 28}

Expected: 422, error mentions email format.
Assertion: response.body.error.details[0].field == "email"

Negative Test 3 — Wrong type

Scenario: Age is a string instead of a number.

Request body: {"name": "Anita", "email": "anita@example.com", "age": "twenty-eight"}

Expected: 422, error mentions age type.

Negative Test 4 — Empty body

Scenario: POST with no body at all.

Request: POST /users with no body and no Content-Type.

Expected: 400 Bad Request, "missing body".

Negative Test 5 — Malformed JSON

Scenario: Body is not valid JSON.

Request body: {name: "Anita",} (unquoted key, trailing comma)

Expected: 400 Bad Request, "invalid JSON".

Negative Test 6 — Wrong Content-Type

Scenario: Send JSON body with Content-Type: text/plain.

Expected: 415 Unsupported Media Type.

Negative Test 7 — Unauthorized

Scenario: POST /users with no Authorization header (assuming auth is required).

Expected: 401 Unauthorized.

Negative Test 8 — Forbidden

Scenario: Authenticated non-admin tries to delete another user.

Request: DELETE /users/456 with valid token for user 123.

Expected: 403 Forbidden.

Negative Test 9 — Resource not found

Scenario: GET a user ID that does not exist.

Request: GET /users/999999

Expected: 404 Not Found.

Negative Test 10 — Duplicate

Scenario: Create a user with an email that already exists.

Expected: 409 Conflict, "email already registered".

The Negative Test Checklist

For every endpoint, ask these questions:

  1. What if a required field is missing?
  2. What if a field has the wrong type?
  3. What if a string field is empty?
  4. What if a string field is too long (e.g., 10000 characters)?
  5. What if a numeric field is negative when it should be positive?
  6. What if a numeric field exceeds the max (e.g., age = 200)?
  7. What if an email/URL field has an invalid format?
  8. What if the body is empty?
  9. What if the JSON is malformed?
  10. What if the Content-Type is wrong?
  11. What if auth is missing?
  12. What if auth is expired?
  13. What if the user lacks permission?
  14. What if the resource does not exist?
  15. What if the resource already exists (duplicates)?
  16. What if the request violates a business rule (e.g., transfer more than balance)?

Common Mistakes

  1. Skipping negative tests. "It works for valid input" is not enough. Most production bugs come from invalid input.
  2. Asserting only the status code. A 422 is good, but the error message should also be useful. Assert the error code and the field reference.
  3. Forgetting to verify no side effects. A failed POST should not create a partial record. Verify the database is unchanged.
  4. Testing with one invalid value. Try multiple: empty string, very long string, special characters, SQL injection attempts, null.
  5. Not testing the error response format. Errors should follow a consistent format. If one endpoint returns {"error": "..."} and another returns {"message": "..."}, that is a bug.

Practical Exercise (15 minutes)

Pick a POST endpoint on JSONPlaceholder. Write 8 test cases — 1 positive and 7 negative. Run each with cURL or Postman and verify the response. Document any case where the API does not behave as expected (you will likely find at least one — JSONPlaceholder is intentionally lenient).

Mini Challenge

Pick any API you have built or worked with. For each endpoint, list at least 3 negative test scenarios that are currently not tested. How many of those would reveal real bugs if you ran them today?

Key Takeaways

  • Positive tests verify the API works for valid input. Negative tests verify it fails gracefully for invalid input.
  • Both are mandatory. A suite with only positive tests gives false confidence.
  • Negative test checklist: missing fields, wrong types, empty values, too-long values, invalid formats, no auth, expired auth, no permission, not found, duplicates, business rule violations.
  • Assert not just the status code but the error format, error message, and absence of side effects.
Course continuity
Previously: Lesson 32 introduced API testing.
Today: You learned the most important distinction — positive vs negative testing.
Next: Lesson 34 covers boundary and data validation testing in depth.

FAQ

How many negative tests are enough?

For each input field, test: missing, empty, wrong type, too short, too long, invalid format, and boundary values. For each endpoint, add: no auth, expired auth, no permission, not found, duplicate. A typical endpoint has 5–15 negative tests.

Should negative tests verify the exact error message?

Assert the error code (stable identifier) and the field referenced. Avoid asserting the exact human-readable message — those change for legitimate reasons (localization, wording improvements) and would break your tests.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments