[英]BATS assert_failure test not recognising exit 1?
While trying to test a method that checks for 5 seconds whether a GitLab server is running, I am experiencing some difficulties in detecting the error message.在尝试测试一种检查 GitLab 服务器是否正在运行 5 秒的方法时,我在检测错误消息时遇到了一些困难。
check_for_n_seconds_if_gitlab_server_is_running() {
duration=$1
echo "duration=$duration"
running="false"
if [ "$running" == "false" ]; then
echo "ERROR, did not find the GitLab server running within $duration seconds!"
exit 1
fi
}
#!./test/libs/bats/bin/bats
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load 'libs/bats-file/load'
source src/helper.sh
@test "If error is thrown if the GitLab server is not running within 5 seconds after uninstallation." {
# run the tested method
check_for_n_seconds_if_gitlab_server_is_running 4
assert_failure
assert_output --partial "ERROR, did not find the GitLab server running within 4 seconds!"
}
I would expect the test to pass, because the exit 1
is reached and I think it throws a failure.我希望测试通过,因为到达了
exit 1
,我认为它会引发失败。
When the exit 1 is included, the test fails and the output of the test is:当包含出口1时,测试失败,测试的output为:
✗ If error is thrown if the GitLab server is not running within 5 seconds after uninstallation.
✗ 如果 GitLab 服务器在卸载后 5 秒内未运行则抛出错误。
When the exit 1
is commented out, the test fails and the output of the test is:当
exit 1
被注释掉时,测试失败,测试的output为:
✗ If error is thrown if the GitLab server is not running within 5 seconds after uninstallation.
(from function `assert_failure' in file test/libs/bats-assert/src/assert.bash, line 140,
in test file test/long_test_helper.bats, line 17)
`assert_failure' failed
duration=4
ERROR, did not find the GitLab server running within 4 seconds!
-- command succeeded, but it was expected to fail --
output :
--
How can I ensure the test detects error that is thrown?/how should I throw the error/ exit 1
command to ensure the assert_failure
test passes?如何确保测试检测到抛出的错误?/我应该如何抛出 error/
exit 1
命令以确保assert_failure
测试通过?
The issue was that I was trying to run a function from the test function, instead of from a separate bash shell. I found out by reproducing another working example that used the run bash -c
command, which did behave as expected on the same function. So in practice the following works:问题是我试图从测试 function 中运行 function,而不是从单独的 bash shell 中运行。我通过重现另一个使用
run bash -c
命令的工作示例发现,它在同一个 88634084 上的行为符合预期.所以在实践中有以下工作:
#!./test/libs/bats/bin/bats
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load 'libs/bats-file/load'
@test "If error is thrown if the GitLab server is not running within 5 seconds after uninstallation." {
run bash -c "source src/helper.sh && check_for_n_seconds_if_gitlab_server_is_running"
assert_failure
assert_output --partial "ERROR, did not find the GitLab server running within 4 seconds!"
}
check_for_n_seconds_if_gitlab_server_is_running() {
duration=$1
echo "duration=$duration"
running="false"
if [ "$running" == "false" ]; then
echo "ERROR, did not find the GitLab server running within $duration seconds!"
exit 1
fi
}
By calling the function via a the run bash -c
command, the test passes:通过
run bash -c
命令调用 function,测试通过:
✓ If error is thrown if the GitLab server is not running within 5 seconds after uninstallation.
✓ 如果 GitLab 服务器在卸载后 5 秒内未运行则抛出错误。
1 test, 0 failures
1 次测试,0 次失败
If someone is able to include a MWE that works without calling the function from a separate run bash -c
command, but directly as a function from within the test function, please post it as a separate answer.如果有人能够在不从单独
run bash -c
命令调用 function 的情况下包含一个 MWE,而是直接从测试 function 中调用 function,请将其作为单独的答案发布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.