【问题标题】:How to organize your Postman collections and execute them with Newman with GitLab CI?如何使用 Newman 和 GitLab CI 组织您的 Postman 集合并执行它们?
【发布时间】:2019-07-15 15:22:35
【问题描述】:
我正在考虑通过为每个功能创建一个集合来组织我的 Postman API 测试。例如,我们有 2 个功能,集合将保存在 /test/FEAT-01/FEAT-01-test.postman_collection.json 中,另一个保存在 /test/FEAT-02/FEAT-02-test.postman_collection.json 中。通过这种方式,我可以在 git 中分别比较集合并根据需要有选择地执行测试。
但是我希望我的 GitLab CI 使用 Newman 在我的根 test 文件夹下执行我的所有测试。我怎样才能做到这一点?
【问题讨论】:
标签:
automated-tests
gitlab
postman-collection-runner
newman
【解决方案1】:
对我有用的一件事是,在 gitlab 中为每个集合创建一个作业,它看起来像这样:
variables:
POSTMAN_COLLECTION1: <Your collection here>.json
POSTMAN_COLLECTION2: <Your collection here>.json
POSTMAN_ENVIRONMENT: <Your environment here>.json
DATA: <Your iterator file>
stages:
- test
postman_tests:
stage: test
image:
name: postman/newman:alpine
entrypoint: [""]
script:
- newman --version
- npm install -g newman-reporter-html
- newman run ${POSTMAN_COLLECTION1} -e ${POSTMAN_ENVIRONMENT} -d ${DATA} --reporters cli,html --reporter-html-export report.html
artifacts:
when: always
paths:
- report.html
postman_test2:
stage: test
image:
name: postman/newman:alpine
entrypoint: [""]
script:
- newman --version
- npm install -g newman-reporter-html
- newman run ${POSTMAN_COLLECTION2} -e ${POSTMAN_ENVIRONMENT} --reporters cli,html --reporter-html-export report.html
artifacts:
when: always
paths:
- report.html
您还可以创建一个脚本,该脚本从您的根文件夹读取并在每个集合中执行 X 次,如下所示:
$ for collection in ./PostmanCollections/*; do newman run "$collection" --environment ./PostmanEnvironments/Test.postman_environment.json' -r cli; done