[Django]-How can I disable a third party API when executing Django unit tests?

1👍

The best way to go about it is probably as you suggest, creating the api object outside and passing it to the constructor. This will allow you to replace the api class easily for testing.

You could create a MockPyChimp class, which has the same methods as the actual PyChimp api. This way you could easily reuse the same mock class across your tests.

I’m not familiar enough with python/django unit testing to be able to suggest any specific libraries to assist in this, but I would assume there exists some sort of mocking libraries or such.

0👍

Well the best way to “mock” a function call in Python is with the mock library. Also if you are using nose as your unit-test framework then a plugin worth considering is nose-blockage which ensures no API calls get through if your tests don’t properly mock out everything.

Leave a comment