我想创建一个在特定代码库上运行各种测试(单元测试,集成测试,api测试等)的bash脚本。我想强制执行此脚本,以在每个构建上运行所有测试,并让构建仅在至少一项测试运行失败时才失败。
我有一个可行的解决方案,但对我来说却很糟糕。如果有人有想法来证明这一点,我将不胜感激。
#!/usr/bin/env bash
set -e
#...
#some other code which should let the build fail immediately if something is wrong
set +e
runUnitTests.sh
unitTestsFailed=$?
runIntegrationTests.sh
integrationTestsFailed=$?
runApiTests.sh
apiTestsFailed=$?
if [ $unitTestsFailed -ne 0 ] || \
[ $integrationTestsFailed -ne 0 ] || \
[ $apiTestsFailed -ne 0 ]; then
echo "Automated tests failed"
exit 1
else
echo "Automated tests succeeded"
exit 0
fi
答案 0 :(得分:3)
您可以使用一个通用功能运行每个测试脚本,该功能可以检查故障并设置错误标记。
failure=0
testSuite() {
"$@" || failure=1
}
testSuite runUnitTests.sh
testSuite runIntegrationTests.sh
testSuite runApiTests.sh
if ((failure)); then
echo "Automated tests failed" >&2
exit 1
else
echo "Automated tests succeeded" >&2
exit 0
fi
答案 1 :(得分:1)
借助$?
变量,您可以检查最后执行的命令的结果:
./runUnitTests.sh && ./runIntegrationTests.sh && ./runApiTests.sh
if [ $? -eq 0 ]; then
echo "Automated tests succeeded"
else
echo "Automated tests failed"
exit 0
fi