我正在根据https://docs.bazel.build/versions/master/skylark/testing.html为我的项目编写分析时间测试,我想知道报告失败的最有用的方法是什么。
在使用unittest.bzl模块时,我使用了asserts.equals、asserts.true等工具,我发现日志中的错误报告有些缺失。例如,如果asserts.true失败,则错误消息为Expected condition to be true, but was false,没有提到哪一行,也没有提到它预期为真的条件。在一个包含大量测试的文件中,这并不是很有用!我知道可以将消息作为参数添加到这些断言中,但为每个断言定制消息也不太理想。有没有一种方法可以得到断言失败导致的回溯呢?或者访问断言失败的行号/详细信息的任何其他方式?
发布于 2021-02-07 06:11:30
我从您的链接中获取了minimal example,并将latest Skylib版本添加到了一个新的工作区。然后,我更改了_provider_contents_test_impl中的期望值,使测试失败。
下面是完整的输出,请注意包含许多有用信息(文件、行、期望值、实际值)的DEBUG。
$ bazel test //mypkg:myrules_test
DEBUG: /home/user/.cache/bazel/_bazel_user/863abec759a50d843603ddf033727331/external/bazel_skylib/lib/unittest.bzl:351:10: In test _provider_contents_test_impl from //mypkg:myrules_test.bzl: Expected "some valuexxxxx", but got "some value"
INFO: Analyzed target //mypkg:provider_contents_test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
FAIL: //mypkg:provider_contents_test (see /home/user/.cache/bazel/_bazel_user/863abec759a50d843603ddf033727331/execroot/__main__/bazel-out/k8-fastbuild/testlogs/mypkg/provider_contents_test/test.log)
Target //mypkg:provider_contents_test up-to-date:
bazel-bin/mypkg/provider_contents_test.sh
INFO: Elapsed time: 0.108s, Critical Path: 0.04s
INFO: 2 processes: 2 linux-sandbox.
INFO: Build completed, 1 test FAILED, 2 total actions
//mypkg:provider_contents_test FAILED in 0.0s
/home/user/.cache/bazel/_bazel_user/863abec759a50d843603ddf033727331/execroot/__main__/bazel-out/k8-fastbuild/testlogs/mypkg/provider_contents_test/test.log
INFO: Build completed, 1 test FAILED, 2 total actions还支持Tracebacks,请参阅this bug report示例。
选项,实际值)。
$ bazel test //mypkg:myrules_test
DEBUG: /home/user/.cache/bazel/_bazel_user/863abec759a50d843603ddf033727331/external/bazel_skylib/lib/unittest.bzl:351:10: In test _provider_contents_test_impl from //mypkg:myrules_test.bzl: Expected "some valuexxxxx", but got "some value"
INFO: Analyzed target //mypkg:provider_contents_test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
FAIL: //mypkg:provider_contents_test (see /home/user/.cache/bazel/_bazel_user/863abec759a50d843603ddf033727331/execroot/__main__/bazel-out/k8-fastbuild/testlogs/mypkg/provider_contents_test/test.log)
Target //mypkg:provider_contents_test up-to-date:
bazel-bin/mypkg/provider_contents_test.sh
INFO: Elapsed time: 0.108s, Critical Path: 0.04s
INFO: 2 processes: 2 linux-sandbox.
INFO: Build completed, 1 test FAILED, 2 total actions
//mypkg:provider_contents_test FAILED in 0.0s
/home/user/.cache/bazel/_bazel_user/863abec759a50d843603ddf033727331/execroot/__main__/bazel-out/k8-fastbuild/testlogs/mypkg/provider_contents_test/test.log
INFO: Build completed, 1 test FAILED, 2 total actions还支持Tracebacks,请参阅this bug report示例。
请注意,asserts.equals将在失败时打印实际值,但asserts.true不会-但您可以通过使用asserts.equals(env, True, test_val, msg)轻松地解决此问题。
https://stackoverflow.com/questions/64354606
复制相似问题