如何获得货物测试到漂亮的打印故障输出
我有一个生锈测试,它正在比较实现partialeq
和debug
的两种类型的结构。
根据货物测试的帮助页面
输出,应该可以将左右参数“漂亮地打印”到每个失败的断言中:
$ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很有问题,因为 assert_eq!()本身打印内容和
货物测试
获得已构造的字符串。也不可能具有宏观的特征,并且在
测试下
非常打印变量,因为std
的代码无法特征(当前)。但是,您可以编写宏来执行此操作,也可以使用库,例如 /a>。
但是请注意,有RFC可以改善
servert!()
宏(通常不认为是破坏的变化),因此将来也可以修复这种情况:This is quite problematic since
assert_eq!()
itself prints the contents andcargo test
gets already-formatted string.It is also impossible to feature-gate the macro and under
test
pretty-print the variables, becausestd
'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: