I am insert data in postman and i want add validation

I am inserting data in postman, Data inserting.
I want to apply validation from POST method in a “TEST SCRIPT”.

I am new to Postman, I am not able to understand

I am writing my json down blow

{
  "firstname": "pradeep",
  "lastname": "tiwari",
  "mobile_no": 99000000000
  "email": "pradeep@gmail.com",
  "user_type": 4,
  "password": "pradeep1",
  "confirmPassword":"pradeep1",
  "dob":"nanana",
  "u_org":25,
  "isfoundingpartner":"undifine",
  "gender":1
}

Hey @pradeep49,

Welcome to the community, it’s great to see you here! :trophy:

Have no fear - We’re all here to help you learn and understand. :heart:

What would you like to validate? Once you know the answer to that question, writing tests for that will become a lot clearer.

I see that you have posted your Request body, what’s returned in the Response?

These are a couple of resources that will explain a little about writing tests in Postman:

1 Like

I am getting a response:

{
    "result": {
        "code": 200,
        "status": true,
        "message": "User successfully registered"
    }
}

One more thing, if I leave a field empty in the concert, then its validation gives me a response in the show like I leave Blanken on the stage.

Example:-

{
  "firstname": "",
  "lastname": "tiwari",
  "mobile_no": 99000000000
  "email": "pradeep@gmail.com",
  "user_type": 4,
  "password": "pradeep1",
  "confirmPassword":"pradeep1",
  "dob":"nanana",
  "u_org":25,
  "isfoundingpartner":"undifine",
  "gender":1
}

I get response

{
    "result": {
        "code": 112,
        "status": false,
        "message": "Firstname can not be empty"
    }
}

If you have a script, then tell me how do I show this message in ‘Test Results’
If this message is shown in ‘Test Result’, then I will not have to get validation
I want to show in the “Message” in “Test Results”
thank you

You could use something like this to check response data on a successful user registration:

pm.test("Successfully register a user", function () {
    let jsonData = pm.response.json().result
    pm.expect(jsonData.code).to.eql(200)
    pm.expect(jsonData.status).to.be.true
    pm.expect(jsonData.message).to.eql("User successfully registered")
})

It’s setting the results object in the response, as the jsonData variable. Then using the pm.expect() function, it’s checking the different values to ensure that these are returned correctly.

The pm.expect() function works with the [chaijs](https://www.chaijs.com/api/bdd/) assertion library that contains certain getters that chain together to construct assertions.

If you’re also looking to add a set of negative type tests to check that these failed cases are working as you expect them too, I would add new requests to the Collection and have these in a separate folder to the positive type tests.

Using the method above, you should be able to construct some tests to check for these different scenarios.

You could also look for things like:

  • Other different blank fields
  • All blank fields
  • Missing Keys
  • Additional Keys
  • The correct data types (swap string values for integers and vice versa)
  • null and undefined values
  • Different HTTP Methods and how these are handled (Is it just expecting you to POST something)
  • The actual response code returned (Should a successfully created user be a 200 or a 201)
  • etc.

This is not an exhaustive list but just some areas that you could create additional tests to check the data.

1 Like

@dannydainton My problem is solved by your reply
I was engaged in it for 2 days before asking question
i added if and else statement in your replay
Got what i want
I got the task at the postman Doing the same Right now i have more questions but
Thanks again