You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Gitlab CI向Postman的Newman测试任务传递变量值

解决方案

第一步:配置Gitlab CI变量

两种配置方式二选一即可:

  • 方式1:页面配置(适合敏感变量)
    进入对应Gitlab项目 → 左侧菜单「设置」→ 「CI/CD」→ 展开「变量」板块 → 点击「添加变量」,键填写UNAME,值填写你需要传入的实际内容,敏感值可勾选「掩码变量」避免日志泄露。
  • 方式2:yml文件直接配置(适合非敏感公开变量)
    在gitlab-ci.yml中添加job级别或全局的variables配置,示例:
    smoketest-test:
      stage: smoketests-test
      variables:
        UNAME: "abc" # 可根据需要调整值
    

第二步:修正newman执行命令

你当前的newman命令存在两处问题:

  1. 命令关键字应为newman run而非newman runs
  2. 传递自定义变量需要使用--env-var参数,直接写变量键值对newman无法识别

修改后的script段的newman命令如下:

newman run tests/postmanui-tests.json -e tests/${ENV_VAR}_environment.json --env-var "uname=${UNAME}"

修改后的完整gitlab-ci.yml示例

smoketest-test:
  stage: smoketests-test
  dependencies:
  - deploy-test
  image: 
    name: postman/newman:alpine
  variables:
    # 非敏感变量可以直接写在这里,敏感变量走Gitlab页面配置即可省略该字段
    UNAME: "your_default_value"
  script:
    - newman --version
    - npm install -g newman-reporter-html
    - newman run tests/postmanui-tests.json -e tests/${ENV_VAR}_environment.json --env-var "uname=${UNAME}"
  artifacts:
     when: always
     paths:
      - report.html
  only:
    - master
  except:
    - schedules
  tags:
    - environment_test

验证逻辑

确保你的Postman测试集合中已经定义了uname变量,可通过pm.environment.get("uname")或者pm.variables.get("uname")获取到传入的值。

内容的提问来源于stack exchange,提问作者ankur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 10:57:04