// Status code tests
pm.test("Status code is 200", function () \\\\{
pm.response.to.have.status(200);
\\\\});
pm.test("Status code name has string", function () \\\\{
pm.response.to.have.status("OK");
\\\\});
// Response time test
pm.test("Response time is less than 200ms", function () \\\\{
pm.expect(pm.response.responseTime).to.be.below(200);
\\\\});
// Header tests
pm.test("Content-Type is present", function () \\\\{
pm.response.to.have.header("Content-Type");
\\\\});
pm.test("Content-Type is application/json", function () \\\\{
pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");
\\\\});
```_
### JSON Essais de réponse
```javascript
// Parse JSON response
const responseJson = pm.response.json();
// Test JSON structure
pm.test("Response has required fields", function () \\\\{
pm.expect(responseJson).to.have.property("id");
pm.expect(responseJson).to.have.property("name");
pm.expect(responseJson).to.have.property("email");
\\\\});
// Test specific values
pm.test("User ID is correct", function () \\\\{
pm.expect(responseJson.id).to.eql(123);
\\\\});
pm.test("Email format is valid", function () \\\\{
pm.expect(responseJson.email).to.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);
\\\\});
// Test array responses
pm.test("Response is an array", function () \\\\{
pm.expect(responseJson).to.be.an('array');
\\\\});
pm.test("Array has correct length", function () \\\\{
pm.expect(responseJson).to.have.lengthOf(5);
\\\\});
```_
### Extraction variable
```javascript
// Extract data from response
const responseJson = pm.response.json();
// Set environment variables
pm.environment.set("userId", responseJson.id);
pm.environment.set("userToken", responseJson.token);
// Extract from headers
const location = pm.response.headers.get("Location");
pm.environment.set("resourceUrl", location);
// Extract using regex
const responseText = pm.response.text();
const match = responseText.match(/token:\s*"([^"]+)"/);
if (match) \\\\{
pm.environment.set("extractedToken", match[1]);
\\\\}
Collection Runner
Recouvrement
// Collection variables
pm.collectionVariables.set("baseUrl", "https://api.example.com");
pm.collectionVariables.get("baseUrl");
// Data-driven testing
// Use CSV or JSON files for test data
// Access data using pm.iterationData.get("fieldName")
// Workflow control
postman.setNextRequest("Request Name");
postman.setNextRequest(null); // Stop execution