Detailed example of Positive and Negative Testing in Postman

Detailed example of Positive and Negative Testing in Postman

When testing an API, it’s crucial to verify how it handles valid requests and responds to incorrect inputs. This process, known as positive and negative testing, ensures that the API behaves as expected in both ideal and edge cases.

Here, I present detailed examples of positive and negative testing in Postman, using a simple API callback function that updates a “notes” field. We’ll cover:

✅ A valid update scenario that successfully updates the notes field.
Three negative test cases where the request fails due to missing data, incorrect field names, or invalid data types.

Each example includes:

  • The request setup (endpoint, headers, and body)
  • The Postman test script to validate responses
  • The expected API response and status codes

/update-notes API Callback Function

API Callback Function

The callback function above focuses on covering these 2 edge cases:

  • Checks if notes exists → If it’s missing, it returns a 400 Bad Request error.
  • Ensures notes is a string → Prevents updating with invalid data.

Test Cases in Postman

Here is 1 example of positive and 3 examples of negative testing that we want to set up in Postman:

ScenarioRequest BodyExpected Response
Valid update{ "notes": "This is my updated note" }Status: 200 OK
Response:
{ "message": "Notes updated successfully" }
Missing notes{}Status: 400 Bad Request
Response:
{ "message": "Invalid request: 'notes' field is required and must be a string" }
Wrong field name{ "notees": "Wrong key" }Status: 400 Bad Request
Response:
{ "message": "Invalid request: 'notes' field is required and must be a string" }
Wrong data type{ "notes": 12345 }Status: 400 Bad Request
Response:
{ "message": "Invalid request: 'notes' field is required and must be a string" }

My test approach is to make a separate request for each of these test cases.

Valid update test setup

Request: POST
Endpoint: http://localhost:5002/api/update-notes
Headers:
Key: X-Test-Auth
Value: test2:test123

Body:

{ "notes": "This is my updated note" }

Test script:

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response should be in JSON format", function () {
    pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});

pm.test("Response contains a success message", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property("message", "Notes updated successfully");
});

pm.test("Response contains the correct username and email", function () {
    var jsonData = pm.response.json();
    
    pm.expect(jsonData).to.have.property("updatedFor");
    pm.expect(jsonData.updatedFor).to.have.property("username", "test665");
    pm.expect(jsonData.updatedFor).to.have.property("email", "[email protected]");
});

pm.test("Response contains the updated notes field", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property("notes", "This is my updated note");
});

Test results:

PASSED Status code is 200
PASSED Response should be in JSON format
PASSED Response contains a success message
PASSED Response contains the correct username and email
PASSED Response contains the updated notes field

Expected response:

{
    "message": "Notes updated successfully",
    "updatedFor": {
        "username": "test665",
        "email": "[email protected]"
    },
    "notes": "This is my updated note"
}

Response status: 200 OK


Missing notes test setup

Request: POST
Endpoint: http://localhost:5002/api/update-notes
Headers:
Key: X-Test-Auth
Value: test2:test123

Body:

{}

Test script:

pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

pm.test("Response contains the Invalid request message", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property("message", "Invalid request: 'notes' field is required and must be a string");
});

Test results:

PASSED Status code is 400
PASSED Response contains the Invalid request message

Expected response:

{ "message": "Invalid request: 'notes' field is required and must be a string" }

Response status: 400 Bad Request


Wrong field name test setup

Request: POST
Endpoint: http://localhost:5002/api/update-notes
Headers:
==> Key: X-Test-Auth
==> Value: test2:test123

Body:

{ "notees": "Wrong key" }

Test script:

pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

pm.test("Response contains the Invalid request message", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property("message", "Invalid request: 'notes' field is required and must be a string");
});

Test results:

PASSED Status code is 400
PASSED Response contains the Invalid request message

Expected response:

{ "message": "Invalid request: 'notes' field is required and must be a string" }

Response status: 400 Bad Request


Wrong data type test setup

Request: POST
Endpoint: http://localhost:5002/api/update-notes
Headers:
==> Key: X-Test-Auth
==> Value: test2:test123

Body:

{ "notes": 12345 }

Test script:

pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

pm.test("Response contains the Invalid request message", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property("message", "Invalid request: 'notes' field is required and must be a string");
});

Test results:

PASSED Status code is 400
PASSED Response contains the Invalid request message

Expected response:

{ "message": "Invalid request: 'notes' field is required and must be a string" }

Response status: 400 Bad Request

Scroll to Top