Dear group,
we’re developing an application that uses Python’s core I/O functions to access the MEDIA_ROOT directory.
A problem is that during tests, MEDIA_ROOT is the same as defined in settings.py and we would much prefer to point it to a temporary directory, that is, do something like MEDIA_ROOT=TemporaryDirectory().
The straightforward approach that is suggested in this Stack Overflow answer doesn’t work well,
because it relies on the temporary directory being implicitly cleaned up (ResourceWarning: Implicitly cleaning up <TemporaryDirectory '/tmp/…'>).
I’ve also found #23251 (Use a temporary folder to store uploaded files during tests) – Django, but the focus there seemed to have shifted from MEDIA_ROOT to STORAGES.
The best solution I’ve come up with so far is:
from pathlib import Path
from tempfile import TemporaryDirectory
from django.conf import settings
from django.test import TestCase, override_settings
class TempDirTest(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Have both the temporary directory and the settings change
# covered in a context.
cls.temp_dir = Path(cls.enterClassContext(TemporaryDirectory()))
cls.enterClassContext(override_settings(MEDIA_ROOT=cls.temp_dir))
def test_example(self):
print(settings.MEDIA_ROOT)
...
This seems to work well, but it took quite a bit of digging to arrive at it. There are many articles about the same problem, but they all seem to be either cumbersome (like mine above feels as well) or come with suble problems like in the above S/O answer.
Therefore my questions:
- Is there a cleaner solution than the one above?
- Could #23251 (Use a temporary folder to store uploaded files during tests) – Django be broadened to cover
MEDIA_ROOTas well?
Best regards,
Carsten