Django 1.3 changed the behavior of models with FileFields such that when the instance is deleted the actual file(s) remain. For most purposes, this is fine. However, I’m testing an application that makes heavy use of ImageFields and I’m ending up with hundreds of useless leftover images in my development upload directory. Since it’s development, it’s not a huge deal, but I’d prefer to clean up after myself in the tests.
What’s the best way to make these images not hang around when testing. I emphasize that part because I don’t want to modify this behavior across the board, using a delete override, etc.
I broke down and compromised a bit. I decided to go ahead and use an overridden
delete, but I made deleting the actual image files dependent on passing ainclude_images=Truekwarg.Then, I modified the
tearDownmethod in my test cases like so:I’m not entirely happy with this solution, but it’s the closest I could get to what I consider ideal. This should really all be handled by the tests themselves.