Junior
Räägi Postmanist ja selle võimalustest API testimisel.
sobes.tech AI
Vastus AI-lt
Postman is a desktop application and web platform for developing and testing APIs. It allows sending HTTP requests, viewing responses, creating request collections, automating testing, documenting APIs, and collaborating on projects.
Main features of Postman for API testing:
- Creating and sending requests: Supports all major HTTP methods (GET, POST, PUT, DELETE, PATCH). Allows configuring request parameters, headers, request body (forms, JSON, XML, etc.).
- Viewing and analyzing responses: Displays status code, headers, and response body. Enables formatting and searching data within the response.
- Working with collections: Organizes requests into folders and categories. Convenient for grouping related tests.
- Variables and environments: Stores and uses variables (URLs, tokens, etc.) in requests. Quickly switch between different testing environments (dev, staging, prod).
- Pre-request and Tests scripts:
- Pre-request scripts: Executed before sending the request. Can be used for data preparation, token generation, etc. Written in JavaScript.
- Tests: Executed after receiving the response. Allows checking status code, headers, response body, response time, and other parameters. Written in JavaScript using the Chai.js framework.
- Automating testing with Collection Runner: Enables running the entire collection or selected requests automatically. You can set execution order, iterations, delays. Results are displayed in a report.
- Integration with CI/CD systems: Ability to run Postman tests from the command line using Newman (CLI runner for Postman) and integrate with Jenkins, GitLab CI, GitHub Actions, and others.
- API documentation: Automatically generates documentation based on the requests in the collection. Convenient for sharing with other teams or clients.
- Collaborative work: Ability to share collections, variables, and environments with other team members.
Example script to check response status and the presence of a field in the response body:
// Check response status
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Check for the presence of the "success" property in the response body (expected to be a boolean)
pm.test("Response body has 'success' property", function () {
const responseJson = pm.response.json();
pm.expect(responseJson).to.have.property('success');
pm.expect(responseJson.success).to.be.a('boolean'); // Optional type check
});
Example of using environment variables:
// Use the {{baseUrl}} variable in the request URL
// In the Pre-request Script, get the token and save it to an environment variable
pm.environment.set("authToken", responseData.token);
// Use the {{authToken}} variable in the Authorization header of the request
Postman greatly simplifies API testing, making it more efficient and convenient.