在运行完所有测试之后,是否可以执行拆解功能? [英] Is there a way to execute a teardown function after all tests have been run?
问题描述
在Rust中,使用标准测试库在所有测试(即cargo test
的末尾)运行后,是否可以执行拆解功能?
In Rust, is there any way to execute a teardown function after all tests have been run (i.e. at the end of cargo test
) using the standard testing library?
我不想在每个测试后运行拆卸功能,因为这些相关文章对此进行了讨论:
I'm not looking to run a teardown function after each test, as they've been discussed in these related posts:
- How to run setup code before any tests run in Rust?
- How to initialize the logger for integration tests?))
这些讨论了运行的想法:
These discuss ideas to run:
- 在每个测试之前进行设置 每项测试之前的拆解(使用
- 设置(使用
std::sync::Once
)
std::panic::catch_unwind
)
在所有测试之前进行- setup before each test
- teardown before each test (using
std::panic::catch_unwind
) - setup before all tests (using
std::sync::Once
)
一种解决方法是将cargo test
调用包装起来的shell脚本,但是我仍然很好奇上述方法是否可行.
One workaround is a shell script that wraps around the cargo test
call, but I'm still curious if the above is possible.
推荐答案
我不确定是否可以通过Rust的内置测试功能.第三方测试系统(例如,发光的或不锈的)可能具有该选项,可能值得研究其确切功能
I'm not sure there's a way to have a global ("session") teardown with Rust's built-in testing features, previous inquiries seem to have yielded little, aside from "maybe a build script". Third-party testing systems (e.g. shiny or stainless) might have that option though, might be worth looking into their exact capabilities
或者,如果每晚比较适合正在实施自定义测试框架功能,您可以使用这个目的.
Alternatively, if nightly is suitable there's a custom test frameworks feature being implemented, which you might be able to use for that purpose.
此外,您可能希望查看macro_rules!
来清理一些样板,这是burntsushi之类的人所做的
That aside, you may want to look at macro_rules!
to cleanup some boilerplate, that's what folks like burntsushi do e.g. in the regex package.
这篇关于在运行完所有测试之后,是否可以执行拆解功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!