Postman Test cases Examples: Learn How to Write API Test cases in Postman ( API Testing Tutorials)
Автор: The Testing Academy
Загружено: 2019-08-11
Просмотров: 82078
Learn How to Write the Postman Test cases Examples. There are number of ways to Write tests in Postman. In this tutorial, I am going to cover how to check the Status Code, Json Data value, Status Message and Many more Testcases in Postman.
To start building test cases quickly, commonly-used snippets are listed next to the test editor. Select a snippet to append the code to the test editor. If needed, update the stub with assertions specific to your endpoint’s expected response. Then, send the request to view the test results at the bottom.
pm.test()
The pm.test() function is used to write test specifications inside the Postman test sandbox. Writing tests inside this function allows you to name the test accurately, and ensures that the rest of the script is not blocked in case of any errors.
Some things to know about the pm.test() function:
The function accepts 2 parameters, the name of the test (as a string) and a function to return a boolean value.
It can be used only in the Tests tab, after the primary Postman request has been sent.
Here are some examples:
// example using pm.response.to.have
pm.test("response is ok", function () {
pm.response.to.have.status(200);
});
// example using pm.expect()
pm.test("environment to be production", function () {
pm.expect(pm.environment.get("env")).to.equal("production");
});
// example using response assertions
pm.test("response should be okay to process", function () {
pm.response.to.not.be.error;
pm.response.to.have.jsonBody("");
pm.response.to.not.have.jsonBody("error");
});
// example using pm.response.to.be*
pm.test("response must be valid and have a body", function () {
// assert that the status code is 200
pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants
// assert that the response has a valid JSON body
pm.response.to.be.withBody;
pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// example using pm.response.to.have
pm.test("response is ok", function () {
pm.response.to.have.status(200);
});
// example using pm.expect()
pm.test("environment to be production", function () {
pm.expect(pm.environment.get("env")).to.equal("production");
});
// example using response assertions
pm.test("response should be okay to process", function () {
pm.response.to.not.be.error;
pm.response.to.have.jsonBody("");
pm.response.to.not.have.jsonBody("error");
});
// example using pm.response.to.be*
pm.test("response must be valid and have a body", function () {
// assert that the status code is 200
pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants
// assert that the response has a valid JSON body
pm.response.to.be.withBody;
pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed
});
There are also other helpers to use in conjunction with pm.test().
pm.expect()
The pm.expect() assertion function was built on the shoulders of the popular JavaScript test library ChaiJS BDD. Using a similar syntax, pm.expect() makes it easy to write readable tests, and you can deal with assertions of data from a response or variables.
🚀 Tools and services I recommend:
Some of the courses that I recommend to become better Automation Tester 🙌🙌
✅Selenium Training and Certification -
https://scrolltest.com/go/edureka
✅Learn Jenkins for QA -
https://bit.ly/learnjenkins-p1
✅Programming Java -
https://bit.ly/learnjava2020-p1
✅Test Automation -
https://bit.ly/learnautomation2020
✅API Testing -
https://www.learnapitesting.com
✅Cypress Tutorial with LIVE Projects -
http://cypresstutorial.com
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: