如何获得货物测试到漂亮的打印故障输出

发布于 2025-02-04 09:40:03 字数 2468 浏览 5 评论 0原文

我有一个生锈测试,它正在比较实现partialeqdebug的两种类型的结构。

根据货物测试的帮助页面输出,应该可以将左右参数“漂亮地打印”到每个失败的断言中:

$ cargo test_foo --lib -- --help

warning: `google2005` (lib test) generated 4 warnings
    Finished test [unoptimized + debuginfo] target(s) in 0.07s
     Running unittests (target/debug/deps/foo)
Usage: test_foo [OPTIONS] [FILTERS...]

Options:
        --include-ignored 
                        Run ignored and not ignored tests
        --ignored       Run only ignored tests
...
***** snipped *****
...
        --format pretty|terse|json|junit
                        Configure formatting of output:
                        pretty = Print verbose output;
                        terse = Display one character per test;
                        json = Output a json document;
                        junit = Output a JUnit document

...
***** snipped *****

我希望这会以适当的凹痕打印我的结构,例如当您使用{:#?} debug输出的漂亮打印标记。但这似乎没有任何效果:

$ cargo test test_foo --lib -- --format pretty
   Finished test [unoptimized + debuginfo] target(s) in 0.07s
     Running unittests (target/debug/deps/foo)

running 1 test
test parser::test::test_walk_scrapes_description ... FAILED

failures:

---- parser::test::test_walk_scrapes_description stdout ----
thread 'parser::test::test_walk_scrapes_description' panicked at 'assertion failed: `(left == right)`
  left: `Cat { name: "Fluffy", age: 8, breed: "Siamese", color: "Brown", weight: 12, chip_number: 10001001 }`,
 right: `Cat { name: "Fluffy", age: 8, breed: "Siamese", color: "Brown", weight: 13, chip_number: 10010001 }`', src/parser.rs:170:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    parser::test::test_walk_scrapes_description

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 13 filtered out; finished in 0.00s 

我认为可以实现这样的目标:

  left: `Cat {
             name: "Fluffy",
             age: 8,
             breed: "Siamese",
             color: "Brown",
             weight: 12,
             chip_number: 10001001
 }`,
 right: `Cat {
             name: "Fluffy",
             age: 8,
             breed: "Siamese",
             color: "Brown",
             weight: 13,
             chip_number: 10010001
 }`',

我已经尝试了Rustc 1.59.0(9D1B2106E 2022-02-23)和<代码> rustc 1.63.0夜利(A6B8C6954 2022-06-03)。

I have a Rust test that is comparing two structs of a type that implements PartialEq and Debug.

According to the help page for the cargo test output, it should be possible to "pretty print" the left and right arguments to each failed assertion:

$ cargo test_foo --lib -- --help

warning: `google2005` (lib test) generated 4 warnings
    Finished test [unoptimized + debuginfo] target(s) in 0.07s
     Running unittests (target/debug/deps/foo)
Usage: test_foo [OPTIONS] [FILTERS...]

Options:
        --include-ignored 
                        Run ignored and not ignored tests
        --ignored       Run only ignored tests
...
***** snipped *****
...
        --format pretty|terse|json|junit
                        Configure formatting of output:
                        pretty = Print verbose output;
                        terse = Display one character per test;
                        json = Output a json document;
                        junit = Output a JUnit document

...
***** snipped *****

I would expect this would print my structs with proper indentation, like when you use the {:#?} pretty print marker for Debug output. But it doesn't seem to have any effect:

$ cargo test test_foo --lib -- --format pretty
   Finished test [unoptimized + debuginfo] target(s) in 0.07s
     Running unittests (target/debug/deps/foo)

running 1 test
test parser::test::test_walk_scrapes_description ... FAILED

failures:

---- parser::test::test_walk_scrapes_description stdout ----
thread 'parser::test::test_walk_scrapes_description' panicked at 'assertion failed: `(left == right)`
  left: `Cat { name: "Fluffy", age: 8, breed: "Siamese", color: "Brown", weight: 12, chip_number: 10001001 }`,
 right: `Cat { name: "Fluffy", age: 8, breed: "Siamese", color: "Brown", weight: 13, chip_number: 10010001 }`', src/parser.rs:170:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    parser::test::test_walk_scrapes_description

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 13 filtered out; finished in 0.00s 

I thought it was possible to do achieve something like this:

  left: `Cat {
             name: "Fluffy",
             age: 8,
             breed: "Siamese",
             color: "Brown",
             weight: 12,
             chip_number: 10001001
 }`,
 right: `Cat {
             name: "Fluffy",
             age: 8,
             breed: "Siamese",
             color: "Brown",
             weight: 13,
             chip_number: 10010001
 }`',

I've tried both with rustc 1.59.0 (9d1b2106e 2022-02-23) and rustc 1.63.0-nightly (a6b8c6954 2022-06-03).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2025-02-11 09:40:03

这很有问题,因为 assert_eq!()本身打印内容货物测试获得已构造的字符串。

也不可能具有宏观的特征,并且在测试下非常打印变量,因为std的代码无法特征(当前)。

但是,您可以编写宏来执行此操作,也可以使用库,例如​​ /a>。

但是请注意,有RFC可以改善servert!()宏(通常不认为是破坏的变化),因此将来也可以修复这种情况:

This is quite problematic since assert_eq!() itself prints the contents and cargo test gets already-formatted string.

It is also impossible to feature-gate the macro and under test pretty-print the variables, because std's code cannot be feature-gated (currently).

You can however write your macro to do that, or use a library like pretty-assertions.

Note, however, that there were RFCs to improve the display of the assert!() macros (it is not generally considered a breaking change), so it is possible that this will also be fixed in the future:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文