Skip to content

Conversation

bemoody
Copy link
Collaborator

@bemoody bemoody commented Sep 20, 2022

When writing files as part of test cases, write them to a temporary
directory (created in setUpClass and destroyed in tearDownClass)
rather than writing them to the current working directory.

This allows running tests without modifying the working tree. It
enables running tests in parallel using pytest-xdist, which allows
running the whole test suite in around 12 seconds rather than 35
seconds.

It's also just cleaner not to dump a lot of temporary files in the
working tree.

It does feel weird to be creating and deleting temporary directories
in class methods, but it seems to work. (The alternative, using
setUp and tearDown, would mean creating a new directory for each
individual test function.)

When writing files as part of test cases, write them to a temporary
directory (created in setUpClass and destroyed in tearDownClass)
rather than writing them to the current working directory.

This allows running tests without modifying the working tree, which
means multiple test processes can run simultaneously from the same
working tree (e.g., using pytest-xdist).
@tompollard
Copy link
Member

Looks great. tempfile is new to me. Good to know!

@tompollard tompollard merged commit 3047c17 into main Sep 20, 2022
@tompollard tompollard deleted the tests-tmpdir branch September 20, 2022 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants