【问题标题】:Cucumber tests fail but travis build still passes黄瓜测试失败但 travis 构建仍然通过
【发布时间】:2019-05-20 13:55:38
【问题描述】:

我正在使用 Travis 进行 CI。出于某种原因,即使某些测试失败,构建也会通过。在此处查看完整日志

https://travis-ci.org/msm1089/hobnob/jobs/534173396

我运行测试的方式是通过由 yarn 运行的 bash 脚本 e2e.test.sh。

搜索此特定问题并没有找到任何有用的信息。我相信这与退出代码有关。我想我需要以某种方式让构建以非零值退出,但正如您在日志底部看到的那样,纱线以 0 退出。

e2e.test.sh

#!/usr/bin/env bash

RETRY_INTERVAL=${RETRY_INTERVAL:-0.2}

# Run our API server as a background process
if [[ "$OSTYPE" == "msys" ]]; then
        if ! netstat -aon | grep "0.0.0.0:$SERVER_PORT" | grep "LISTENING"; then
                pm2 start --no-autorestart --name test:serve "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" -- run test:serve
                until netstat -aon | grep "0.0.0.0:$SERVER_PORT" | grep "LISTENING"; do
                sleep $RETRY_INTERVAL
                done
        fi
else
        if ! ss -lnt | grep -q :$SERVER_PORT; then
                yarn run test:serve &
        fi
        until ss -lnt | grep -q :$SERVER_PORT; do
          sleep $RETRY_INTERVAL
        done
fi
npx cucumber-js spec/cucumber/features --require-module @babel/register --require spec/cucumber/steps

if [[ "$OSTYPE" == "msys" ]]; then
        pm2 delete test:serve
fi

travis.yml

language: node_js
node_js:
  - 'node'
  - 'lts/*'
  - '10'
  - '10.15.3'
services:
  - elasticsearch
before_install:
  - curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.deb
  - sudo dpkg -i --force-confnew elasticsearch-6.6.1.deb
  - sudo service elasticsearch restart
before_script:
  - sleep 10
env:
  global:
    - NODE_ENV=test
    - SERVER_PROTOCOL=http
    - SERVER_HOSTNAME=localhost
    - SERVER_PORT=8888
    - ELASTICSEARCH_PROTOCOL=http
    - ELASTICSEARCH_HOSTNAME=localhost
    - ELASTICSEARCH_PORT=9200
    - ELASTICSEARCH_INDEX=test

package.json

...
scripts:{
    "test": "yarn run test:unit && yarn run test:integration && yarn run test:e2e"
}
...

那么,如何确保黄瓜退出代码是返回的代码,以便在测试未通过时构建失败?

【问题讨论】:

    标签: node.js bash cucumber travis-ci


    【解决方案1】:

    有几种可能的方法可以解决这个问题。这是我最喜欢的两个。

    选项 1:

    在您的 bash 脚本顶部添加 set -e,以便它在第一个错误时退出,保留退出代码,如果它不为零,则随后使 Travis 失败。

    选项 2:

    捕获您想要的任何退出代码,并在任何有意义的地方退出。

    run whatever command here
    exitcode=$?
    [[ $exitcode == 0 ]] || exit $exitcode
    

    附带说明 - 您的 bash 脚本似乎有太多责任。如果可能的话,我会考虑将它们分开,然后你给 travis 一个要运行的命令列表,可能还有一两个 before_script 命令。

    类似的东西:

    # .travis.yml
    before_script:
    - ./start_server.sh
    
    script:
    - npx cucumber-js spec/cucumber/features ...
    

    【讨论】:

    • 非常感谢@DannyB - 我使用了选项 1,效果很好!我确实需要在测试过程中做一些重构!
    猜你喜欢
    • 2016-06-25
    • 1970-01-01
    • 2023-03-19
    • 2017-09-23
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多