Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to execute a teardown function after all tests have been run?

Tags:

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:

  • setup before each test
  • teardown before each test (using std::panic::catch_unwind)
  • setup before all tests (using 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.

like image 480
Ivan Gozali Avatar asked Nov 27 '19 20:11

Ivan Gozali


People also ask

Does teardown run after every test?

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..

Where will you use setup and tear down methods and make it run once for all the tests in the 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.

What does the teardown () function do?

tearDown()Provides an opportunity to perform cleanup after each test method in a test case ends.

Where will you use setup () and teardown () methods?

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 .


1 Answers

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.

like image 64
Masklinn Avatar answered Sep 23 '22 18:09

Masklinn