Newman cho Tester: Từ Postman Test đến HTML Test Report
Khi làm API testing, Postman là một trong những công cụ phổ biến nhất để gửi request và kiểm tra response.
Nhưng khi số lượng API test case tăng lên, việc mở Postman và chạy từng request thủ công sẽ trở nên mất thời gian.
Đây là lúc Newman trở nên hữu ích.
Bài viết này sẽ hướng dẫn từ đầu cách một Tester có thể:
Hiểu Newman là gì
Cài đặt Newman
Tạo API assertions trong Postman
Export Postman Collection
Chạy Collection bằng Newman
Sử dụng Environment
Tạo HTML test report
Xử lý một số lỗi thường gặp
Note: Các API, endpoint, event name và test data trong bài chỉ là dữ liệu minh họa.
1. Newman là gì?
Newman là command-line collection runner của Postman.
Nói đơn giản:
Postman giúp Tester xây dựng và chạy API test, còn Newman giúp Tester chạy Postman Collection từ command line.
Ví dụ, thay vì mở Postman và chạy Collection bằng UI:
newman run "API Test Collection.postman_collection.json"
Newman sẽ:
- Đọc Collection.
- Gửi các API request.
- Chạy các test script/assertions.
- Xác định test Pass/Fail.
- Xuất kết quả ra terminal hoặc report.
2. Vì sao Tester nên biết Newman?
Giả sử bạn có 50 API test cases.
Nếu chạy thủ công:
Open Postman
↓
Run request
↓
Check result
↓
Run next request
↓
Repeat...
Với Newman:
Postman Collection
↓
Newman
↓
Execute all tests
↓
Test Results
↓
HTML Report
Điều này đặc biệt hữu ích khi bạn cần:
Regression testing
Chạy lại nhiều API test cases
Kiểm tra API sau khi developer deploy build mới
Lưu lại test execution evidence
Chia sẻ test result với team
3. Newman có thay thế Postman không?
Không.
Hai công cụ phục vụ hai mục đích khác nhau.
| Tool | Vai trò |
|---|---|
| Postman | Tạo request và viết API tests |
| Collection | Lưu requests và test scripts |
| Environment | Quản lý variables |
| Newman | Execute Collection từ command line |
| HTML Reporter | Tạo report |
Có thể hình dung:
Tester
│
▼
Postman
│
├── API Request
├── Test Script
└── Collection
│
▼
Newman
│
▼
Test Result
│
▼
HTML Report
4. Chuẩn bị môi trường
Bạn cần:
Postman
Node.js
npm
Newman
Newman HTML Reporter
Kiểm tra Node.js
Mở Command Prompt hoặc Terminal:
node -v
Sau đó:
npm -v
Nếu cả hai command đều trả về version, môi trường Node.js đã sẵn sàng.
5. Cài đặt Newman
Chạy:
npm install -g newman
Sau khi cài xong:
newman -v
Nếu command trả về version của Newman, quá trình cài đặt đã thành công.
6. Cài HTML Reporter
Newman có thể hiển thị kết quả trực tiếp trên terminal.
Tuy nhiên, đối với Tester, một HTML report sẽ thuận tiện hơn khi cần review hoặc chia sẻ kết quả.
Một reporter phổ biến là:
newman-reporter-htmlextra
Cài đặt:
npm install -g newman-reporter-htmlextra
Kiểm tra:
npm list -g newman-reporter-htmlextra
7. Chuẩn bị API Test trong Postman
Giả sử chúng ta có một API public:
GET https://api.example.com/api/v1/events/public
API này trả về danh sách event:
{
"data": [
{
"id": 101,
"name": "Annual Meeting",
"type": "event",
"start_date": "2026-10-20",
"location": "London",
"chapter_name": "London"
}
]
}
Đây chỉ là API minh họa.
8. Viết Assertions trong Postman
Newman không tự biết API response đúng hay sai.
Tester cần định nghĩa expected result bằng test scripts.
Trong Postman, mở request:
Scripts
↓
Post-response
Ví dụ kiểm tra status code:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Kiểm tra response là JSON:
pm.test("Response is valid JSON", function () {
pm.expect(() => pm.response.json()).not.to.throw();
});
Kiểm tra data tồn tại:
pm.test("Response contains data", function () {
const json = pm.response.json();
pm.expect(json).to.have.property("data");
});
Kiểm tra data là array:
pm.test("data is an array", function () {
const json = pm.response.json();
pm.expect(json.data).to.be.an("array");
});
9. Một API request có thể có nhiều test cases
Tester mới sử dụng Postman đôi khi có xu hướng tạo một request cho mỗi test case.
Ví dụ:
TC-01 - Verify status code
TC-02 - Verify response schema
TC-03 - Verify event data
TC-04 - Verify sorting
TC-05 - Verify security
Nếu tất cả đều kiểm tra cùng một API thì không nhất thiết phải tạo 5 request riêng.
Có thể tổ chức:
Public Events API
└── GET Public Events
│
├── TC-01 Status code
├── TC-02 Response schema
├── TC-03 Event data
├── TC-04 Sorting
└── TC-05 Security
Ví dụ:
pm.test("TC-01 - Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("TC-02 - Response contains data", function () {
const json = pm.response.json();
pm.expect(json).to.have.property("data");
});
pm.test("TC-03 - data is an array", function () {
const json = pm.response.json();
pm.expect(json.data).to.be.an("array");
});
Một request có thể chứa nhiều assertions.
Điều này giúp Collection dễ quản lý hơn.
10. Export Postman Collection
Sau khi hoàn thành API tests, bạn cần export Collection.
Trong Postman:
Collection
↓
...
↓
Export
Chọn JSON format.
Ví dụ file:
API Test Collection.postman_collection.json
11. Chạy Collection bằng Newman
Mở Command Prompt và chuyển đến thư mục chứa Collection.
Ví dụ:
cd /d D:\API_Testing
Kiểm tra file:
dir
Bạn sẽ thấy:
API Test Collection.postman_collection.json
Chạy:
newman run "API Test Collection.postman_collection.json"
Newman sẽ bắt đầu execute toàn bộ Collection.
12. Đọc kết quả Newman
Sau khi chạy xong, Newman sẽ hiển thị summary.
Ví dụ:
iterations: 1
requests: 5
test-scripts: 5
assertions: 18
failures: 0
Có thể hiểu:
iterations: số lần chạy Collection
requests: số API requests được execute
test-scripts: số request có test script
assertions: tổng số assertions
failures: số assertion/request bị fail
Nếu:
failures: 0
có nghĩa là không có assertion nào bị fail trong lần execution đó.
13. Sử dụng Environment
Trong thực tế, không nên hard-code API domain vào từng request.
Ví dụ thay vì:
https://api.example.com/api/v1/events/public
có thể sử dụng:
{{API_DOMAIN}}/api/v1/events/public
Sau đó tạo Environment:
Environment: QA
API_DOMAIN = https://qa-api.example.com
Khi chuyển sang staging:
Environment: Staging
API_DOMAIN = https://staging-api.example.com
Collection vẫn giữ nguyên.
Chỉ cần thay Environment.
Đây là một cách tốt để tránh duplicate Collection cho từng environment.
14. Export Environment
Sau khi tạo Environment trong Postman, export nó thành JSON.
Ví dụ:
QA Environment.postman_environment.json
Sau đó Newman có thể sử dụng file này.
Command:
newman run "API Test Collection.postman_collection.json" -e "QA Environment.postman_environment.json"
15. Tạo HTML Report
Đây là phần thú vị nhất.
Thay vì chỉ xem kết quả trên terminal:
newman run "API Test Collection.postman_collection.json"
hãy sử dụng HTML reporter:
newman run "API Test Collection.postman_collection.json" -r htmlextra
Newman sẽ execute Collection và generate HTML report.
16. Đặt tên và vị trí cho Report
Bạn có thể chỉ định nơi lưu report:
newman run "API Test Collection.postman_collection.json" ^
-r htmlextra ^
--reporter-htmlextra-export "newman/api-test-report.html"
Trên Windows, dấu ^ cho phép command được viết trên nhiều dòng.
Hoặc viết thành một dòng:
newman run "API Test Collection.postman_collection.json" -r htmlextra --reporter-htmlextra-export "newman/api-test-report.html"
Sau khi chạy xong, mở:
newman/api-test-report.html
bằng browser.
17. Command đầy đủ với Environment
Nếu Collection sử dụng Environment:
newman run "API Test Collection.postman_collection.json" -e "QA Environment.postman_environment.json" -r htmlextra --reporter-htmlextra-export "newman/api-test-report.html"
Đây là command quan trọng cần nhớ:
Collection
+
Environment
+
Newman
+
HTML Reporter
=
HTML Test Report
18. Một lỗi thường gặp: Request URL is empty
Trong quá trình sử dụng Newman, một lỗi dễ gặp là:
request url is empty
Ví dụ Collection chứa:
{
"name": "Verify announcement",
"request": {
"method": "GET",
"url": {
"raw": ""
}
}
}
Request này không có URL.
Khi chạy trong Newman, nó sẽ fail vì Newman thực sự đọc cấu trúc JSON của Collection.
Cách kiểm tra
Nếu Newman báo:
request url is empty
hãy kiểm tra:
Collection có request nào không có URL không?
Request đó có được tạo chỉ để chứa test case không?
Collection JSON có bị export thiếu URL không?
Nếu nhiều test cases cùng kiểm tra một API, hãy cân nhắc đưa chúng vào Post-response script của cùng một request, thay vì tạo các request rỗng.
19. Một lỗi khác: Environment variable không được resolve
Giả sử request:
{{API_DOMAIN}}/api/v1/events/public
nhưng chạy:
newman run "API Test Collection.postman_collection.json"
Newman có thể không có giá trị của:
API_DOMAIN
Khi đó hãy truyền Environment:
newman run "API Test Collection.postman_collection.json" -e "QA Environment.postman_environment.json"
Đây là một lỗi rất phổ biến khi mới bắt đầu sử dụng Newman.
20. HTML Report mang lại gì cho Tester?
Một test report giúp biến kết quả automation thành test execution evidence.
Thay vì chỉ nói:
API testing passed.
Bạn có thể cung cấp:
API Test Execution
Environment: QA
Requests: 20
Assertions: 96
Passed: 94
Failed: 2
HTML report có thể được sử dụng để:
Review test execution
Chia sẻ kết quả với Developer
Gửi cho QA Lead
Lưu test evidence
Điều tra failed test
Hỗ trợ regression testing
21. Newman Cheat Sheet
Install Newman
npm install -g newman
Check Newman
newman -v
Install HTML Reporter
npm install -g newman-reporter-htmlextra
Run Collection
newman run "collection.json"
Run Collection with Environment
newman run "collection.json" -e "environment.json"
Generate HTML Report
newman run "collection.json" -r htmlextra
Generate HTML Report with custom path
newman run "collection.json" -r htmlextra --reporter-htmlextra-export "newman/report.html"
Full command
newman run "collection.json" -e "environment.json" -r htmlextra --reporter-htmlextra-export "newman/api-test-report.html"
22. Final Thoughts
Newman không phải là một công cụ thay thế Postman.
Nó giúp Tester đưa những API tests đã được xây dựng trong Postman sang một hình thức command-line execution.
Workflow cơ bản có thể nhớ như sau:
Requirement
↓
API Test Cases
↓
Postman
↓
Assertions
↓
Export Collection
↓
Newman
↓
HTML Report
Điều quan trọng nhất không phải chỉ là nhớ command:
newman run collection.json
Mà là hiểu được toàn bộ flow:
Requirement → Test Case → Assertion → Execution → Result → Test Evidence
Đó là nền tảng để Tester từng bước chuyển từ API manual testing sang API automation.
