How to use the command 'newman' (with examples)
The newman
command is a collection runner for Postman. It allows you to run Postman collections either from a file or from a URL. This command is useful for automating and integrating Postman collections into your development workflow.
Use case 1: Run a collection (from a file)
Code:
newman run path/to/collection.json
Motivation: Running a collection from a file allows you to easily test and validate your APIs without the need to manually import the collection into Postman. This is particularly useful when you want to automate your test runs or integrate them into your continuous integration (CI) pipeline.
Explanation:
newman run
: This is the command to run a Postman collection using Newman.path/to/collection.json
: This argument specifies the path to the collection file that you want to run.
Example output:
newman
→ GET /api/users
GET 200 OK https://api.example.com/api/users (158ms)
✓ Status code is 200
→ POST /api/users
POST 201 Created https://api.example.com/api/users (217ms)
✓ Status code is 201
┌─────────────────────────┬────────────┬────────────┐
│ │ executed │ failed │
├─────────────────────────┼────────────┼────────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼────────────┼────────────┤
│ requests │ 2 │ 0 │
├─────────────────────────┼────────────┼────────────┤
│ test-scripts │ 2 │ 0 │
├─────────────────────────┼────────────┼────────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼────────────┼────────────┤
│ assertions │ 2 │ 0 │
├─────────────────────────┴────────────┴────────────┤
│ total run duration: 540ms │
├──────────────────────────────────────────────────┤
│ total data received: 130B (approx) │
├──────────────────────────────────────────────────┤
│ average response time: 187ms │
└──────────────────────────────────────────────────┘
Use case 2: Run a collection (from a URL)
Code:
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv
Motivation: Running a collection from a URL allows you to easily collaborate and share collections with your team. You can simply provide the URL to your team members and they can run the collection without the need to manually import it into Postman.
Explanation:
newman run
: This is the command to run a Postman collection using Newman.https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv
: This argument specifies the URL of the collection that you want to run.
Example output:
newman
→ GET /api/users
GET 200 OK https://api.example.com/api/users (158ms)
✓ Status code is 200
→ POST /api/users
POST 201 Created https://api.example.com/api/users (217ms)
✓ Status code is 201
┌─────────────────────────┬────────────┬────────────┐
│ │ executed │ failed │
├─────────────────────────┼────────────┼────────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼────────────┼────────────┤
│ requests │ 2 │ 0 │
├─────────────────────────┼────────────┼────────────┤
│ test-scripts │ 2 │ 0 │
├─────────────────────────┼────────────┼────────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼────────────┼────────────┤
│ assertions │ 2 │ 0 │
├─────────────────────────┴────────────┴────────────┤
│ total run duration: 540ms │
├──────────────────────────────────────────────────┤
│ total data received: 130B (approx) │
├──────────────────────────────────────────────────┤
│ average response time: 187ms │
└──────────────────────────────────────────────────┘
Conclusion:
The newman
command is a powerful tool for running Postman collections either from a file or from a URL. It helps automate API testing and integration into your development workflow.