【问题标题】:Is there a way to improve starlark's unittest error reporting?有没有办法改进 starlark 的 unittest 错误报告?
【发布时间】:2020-10-14 13:40:30
【问题描述】:

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

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

【问题讨论】:

    标签: bazel starlark


    【解决方案1】:

    我从您的链接中获取了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
    
    

    还支持回溯,例如,请参阅 this bug report

    tion,实际值)。

    $ 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
    
    

    还支持回溯,例如,请参阅 this bug report

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-08
      • 2011-02-03
      • 1970-01-01
      • 2013-01-18
      • 2020-03-25
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多