1

我正在根据https://docs.bazel.build/versions/master/skylark/testing.html为我的项目编写分析时间测试,我想知道报告失败的最有用的方法是什么。

使用unittest.bzl模块,我正在使用asserts.equalsasserts.true,发现日志中的错误报告有些缺乏。例如,如果asserts.true失败,则错误消息是Expected condition to be true, but was false,没有提及哪一行,或者它预期为真的条件是什么。在一个包含大量测试的文件中,这不是很有用!我知道可以将消息作为参数添加到这些断言中,但是为每个断言定制消息也感觉不理想。有没有办法获得由断言失败引起的回溯?或者访问断言失败的行号/详细信息的任何其他方式?

4

1 回答 1

0

我从您的链接中获取了最小的示例,并将最新的Skylib 版本添加到新的 WORKSPACE 中。然后我更改了预期值_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

还支持回溯,例如,请参阅此错误报告

化,实际值)。

$ 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

还支持回溯,例如,请参阅此错误报告

请注意,这asserts.equals将在失败时打印实际值,但asserts.true不会 - 但您可以通过使用轻松解决此问题asserts.equals(env, True, test_val, msg)

于 2021-02-06T22:11:30.190 回答