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
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
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
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
Request body:
{"name": "Anita", "email": "anita@example.com", "age": "twenty-eight"}Expected: 422, error mentions age type.
Negative Test 4 — Empty body
Request:
POST /users with no body and no Content-Type.Expected: 400 Bad Request, "missing body".
Negative Test 5 — Malformed JSON
Request body:
{name: "Anita",} (unquoted key, trailing comma)Expected: 400 Bad Request, "invalid JSON".
Negative Test 6 — Wrong Content-Type
Content-Type: text/plain.Expected: 415 Unsupported Media Type.
Negative Test 7 — Unauthorized
Expected: 401 Unauthorized.
Negative Test 8 — Forbidden
Request:
DELETE /users/456 with valid token for user 123.Expected: 403 Forbidden.
Negative Test 9 — Resource not found
Request:
GET /users/999999Expected: 404 Not Found.
Negative Test 10 — Duplicate
Expected: 409 Conflict, "email already registered".
The Negative Test Checklist
For every endpoint, ask these questions:
- What if a required field is missing?
- What if a field has the wrong type?
- What if a string field is empty?
- What if a string field is too long (e.g., 10000 characters)?
- What if a numeric field is negative when it should be positive?
- What if a numeric field exceeds the max (e.g., age = 200)?
- What if an email/URL field has an invalid format?
- What if the body is empty?
- What if the JSON is malformed?
- What if the Content-Type is wrong?
- What if auth is missing?
- What if auth is expired?
- What if the user lacks permission?
- What if the resource does not exist?
- What if the resource already exists (duplicates)?
- What if the request violates a business rule (e.g., transfer more than balance)?
Common Mistakes
- Skipping negative tests. "It works for valid input" is not enough. Most production bugs come from invalid input.
- 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.
- Forgetting to verify no side effects. A failed POST should not create a partial record. Verify the database is unchanged.
- Testing with one invalid value. Try multiple: empty string, very long string, special characters, SQL injection attempts, null.
- 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.
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.
Comments
Comments
Post a Comment