BATS assert_failure 测试无法识别出口 1?
BATS assert_failure test not recognising exit 1?
在尝试测试一种检查 GitLab 服务器是否 运行 的方法时,我在检测错误消息时遇到了一些困难。
检查 GitLab 服务器状态的功能
check_for_n_seconds_if_gitlab_server_is_running() {
duration=
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!"
}
预期行为
我希望测试通过,因为达到 exit 1
并且我认为它会引发失败。
观察到的行为
当包含出口1时,测试失败,测试输出为:
✗ If error is thrown if the GitLab server is not running within 5 seconds after uninstallation.
当exit 1
被注释掉时,测试失败,测试输出为:
✗ 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 :
--
问题
如何确保测试检测到抛出的错误?/我应该如何抛出错误/exit 1
命令以确保 assert_failure
测试通过?
问题是我试图 运行 来自测试函数的函数,而不是来自单独的 bash shell 的函数。我通过重现另一个使用 run bash -c
命令的工作示例发现了这一点,该命令在同一函数上的行为确实符合预期。所以在实践中,以下工作:
测试代码
#!./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=
echo "duration=$duration"
running="false"
if [ "$running" == "false" ]; then
echo "ERROR, did not find the GitLab server running within $duration seconds!"
exit 1
fi
}
预期行为
通过 run bash -c
命令调用函数,测试通过:
✓ If error is thrown if the GitLab server is not running within 5 seconds after uninstallation.
1 test, 0 failures
备注
如果有人能够包含无需从单独的 run bash -c
命令调用函数即可工作的 MWE,而是直接作为测试函数中的函数,请 post 将其作为单独的回答。
在尝试测试一种检查 GitLab 服务器是否 运行 的方法时,我在检测错误消息时遇到了一些困难。
检查 GitLab 服务器状态的功能
check_for_n_seconds_if_gitlab_server_is_running() {
duration=
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!"
}
预期行为
我希望测试通过,因为达到 exit 1
并且我认为它会引发失败。
观察到的行为
当包含出口1时,测试失败,测试输出为:
✗ If error is thrown if the GitLab server is not running within 5 seconds after uninstallation.
当exit 1
被注释掉时,测试失败,测试输出为:
✗ 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 :
--
问题
如何确保测试检测到抛出的错误?/我应该如何抛出错误/exit 1
命令以确保 assert_failure
测试通过?
问题是我试图 运行 来自测试函数的函数,而不是来自单独的 bash shell 的函数。我通过重现另一个使用 run bash -c
命令的工作示例发现了这一点,该命令在同一函数上的行为确实符合预期。所以在实践中,以下工作:
测试代码
#!./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=
echo "duration=$duration"
running="false"
if [ "$running" == "false" ]; then
echo "ERROR, did not find the GitLab server running within $duration seconds!"
exit 1
fi
}
预期行为
通过 run bash -c
命令调用函数,测试通过:
✓ If error is thrown if the GitLab server is not running within 5 seconds after uninstallation.
1 test, 0 failures
备注
如果有人能够包含无需从单独的 run bash -c
命令调用函数即可工作的 MWE,而是直接作为测试函数中的函数,请 post 将其作为单独的回答。