0

I want to validate that material ID has length of "14" and Material department is "OT"

what will be test case to write in postman for below JSON response.

{
  "success": "true",
  "Result": {
    "Response": [
      {
        "Material Id": "e838663839398f",
        "Material Department": "OT"
      }
    ]
  }
}
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
  • Hi, what have you tried so far? The Postman site has a guide starter guide for creating tests. https://learning.postman.com/docs/writing-scripts/test-scripts/ – Chris Adams Jul 28 '21 at 13:27

1 Answers1

2

You could write test like this

const res = pm.response.json();

pm.test("check id", () => {
    const id = res.Result.Response[0]["Material Id"];
    pm.expect(id.length).to.eql(14);
});


pm.test("check Department", () => {
    const dep = res.Result.Response[0]["Material Department"];
    pm.expect(dep).to.eql("OT");
});
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
  • {"Success" : true, "message" : "" ,"Statuscode" : 200 , "Result" : [{"Value" : "F33344555656565" "Text" : "Accident" "Criteria" : "TTL" }] } I want to Validate that Values has length of "15" and Text has "Accident as Values" – Vidit Yadav Jul 29 '21 at 08:19
  • The answer provided by lucasnguyen17 should be enough to to work out your second requirement. – Chris Adams Jul 29 '21 at 09:06
  • I think you can see my answer and find out the way to deal with similar question. – lucas-nguyen-17 Jul 29 '21 at 11:15
  • @lucasnguyen17 by your test case length is not getting validated if i enter lenght as "20" instead of "14" then test case is also passing , test case is even passing if i enter "0" instead of "14" – Vidit Yadav Jul 30 '21 at 06:27
  • @ViditYadav Did you save the changes before running the test? I have tested it before posting the answer. – lucas-nguyen-17 Jul 30 '21 at 06:43
  • @lucasnguyen17 Yes i di save the changes But still all test cases are passing – Vidit Yadav Aug 02 '21 at 11:46
  • @ViditYadav I made a recording to prove my test https://drive.google.com/file/d/1Z9HSbaSN0U-iQjs5h-h2VgjZSDWmcEIA/view?usp=sharing – lucas-nguyen-17 Aug 02 '21 at 14:39
  • Thanks @lucasnguyen17 , your video really helped me – Vidit Yadav Nov 19 '21 at 13:49