我正在使用 Travis 进行 CI。由于某种原因,即使某些测试失败,构建也会通过。请在此处查看完整日志
https://travis-ci.org/msm1089/hobnob/jobs/534173396
我运行测试的方式是通过由yarn运行的bash脚本e2e.test.sh。
搜索此特定问题没有发现任何有帮助的信息。我相信这与退出代码有关。我想我需要以某种方式让构建以非零退出,但正如您在日志底部看到的那样,yarn 以 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"
}
...
那么,如何确保 Cucumber 退出代码是返回的代码,以便在测试未通过时构建失败?
最佳答案
有几种可能的方法可以解决这个问题。这是我最喜欢的两个。
选项 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 ...
关于node.js - cucumber 测试失败,但 travis build 仍然通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56208271/