I am writing unittests and I need to lock one class that works directly with the other server's API. It is clear that during the tests I do not need (or should not even) refer to this api, so I had to lock the class.

There is a class that works with the API:

class Video(model.Models): def _return(self, url, method='GET', **kwargs): ... def delete_video(self, idx): ... ... 

This class is initialized as singleton.

 vimeo_gateway = VimeoGateway() 

I wrote a new class and called it FakeVideo (). There all the same methods as in the original, but they only return the same thing that I indicated.

Next, I initialize the tests. I have a file that is located common / tests

 class TestRunner(DiscoverRunner): @override_settings(REPLICA_NAME='default') def run_tests(self, test_labels, extra_tests=None, **kwargs): with patch('common.vimeo.Video') as mock: mock = FakeVideo() return super(TestRunner, self).run_tests(test_labels, extra_tests, **kwargs) 

But this code does not work. When testing, it still comes into the original class and tries to work with the API directly. What's my mistake?

  • with patch ('common.vimeo.Video', new = FakeVideo)? - m9_psy 9:25 pm

0