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:
These discuss ideas to run:
std::panic::catch_unwind
)std::sync::Once
)One workaround is a shell script that wraps around the cargo test
call, but I'm still curious if the above is possible.
The tearDown runs after each test. Do you have any test in your test file? JUnit will run only tests defined in the current class..
As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete.
tearDown()Provides an opportunity to perform cleanup after each test method in a test case ends.
Prepare and Tear Down State for a Test Class XCTest runs setUp() once before the test class begins. If you need to clean up temporary files or capture any data that you want to analyze after the test class is complete, use the tearDown() class method on XCTestCase .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With