xdem icon indicating copy to clipboard operation
xdem copied to clipboard

Consistently manage temporary test files

Open rhugonnet opened this issue 1 year ago • 2 comments

We now create files during tests. For easy of use and consistency, it would be good to delete files at the end of all tests. Also, we'd need to add fixtures to set up test dependencies in case a certain test is run on its own (to ensure the data needed for it is created).

Original discussion Yes, deleting the files could be run as a final test, when all tests pass. Maybe we can make sure the files are not deleted until the tests pass, so it would not be a problem re-running individual files?

Originally posted by @adehecq in https://github.com/GlacioHack/xdem/pull/292#discussion_r965680786

rhugonnet avatar Sep 08 '22 12:09 rhugonnet

This is normally handled quite easily with a tempfile context, or am I missing a part of the problem?

with tempfile.TemporaryDirectory() as temp_dir:
     # do I/O stuff

This makes sure the tempdir is removed, even in case of a crash. The same without a context manager does not do that:

temp_dir = tempfile.TemporaryDirectory()

# do I/O stuff

erikmannerfelt avatar Aug 16 '23 10:08 erikmannerfelt

We have files created in some tests that are relied on during other tests (I did this to speed up things during the big CI PR), and not compute those things twice, for example: https://github.com/GlacioHack/xdem/blob/main/tests/test_spatialstats.py#L275

The files are created in test order (to avoid using pytest_fixtures between modules/tests, which rapidly gets quite complex) and they are ignored by .gitignore. It's not such a big bother... but maybe a proper clean-up at the end would be better? (it could just be a "pytest_sessionfinish" in conftest.py?)

rhugonnet avatar Aug 16 '23 13:08 rhugonnet