Test Helper Crate

When building tests I often find myself doing the same things...

  • Create some files. Do stuff. Delete those files.
  • Create a folder. Do stuff inside that folder. Delete that folder.
  • et cetera

One very recurring theme is "ignore these errors". An example would be a working directory that several tests use. The create_dir from the second test is going to fail with AlreadyExists. I want to ignore that error but fail on any other errors.

Is there a crate for those sorts of things? What do y'all do?

The tempfile crate is very useful for simple creation (and automatic deletion) of test files and directories.

Note: Rust tests run concurrently by default, so it is often a bad idea to share the same temporary directory (or other resources) between tests. For example, you don’t want one test deleting a shared directory while another is still using it. Using tempfile makes it easy for each test to create a unique/random directory or filename.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.