[Answer]-Write test for views containing os.remove in django

1👍

Yes. It’s called mocking, and there is a Python library for it: mock. Mock is available in the standard library as unittest.mock for Python 3.3+, or standalone for earlier versions.

So you would do something like this:

from mock import patch
...
@patch('mymodel_module.os.remove')
def test_my_method(self, mocked_remove):
    call_my_model_method()
    self.assertTrue(mocked_remove.called)

(where mymodel_module is the models.py where your model is defined, and which presumably imports os.)

Leave a comment