Mocking functions in all Django Test Enviroment

Sometimes, when you’re doing tests for your application you have to be careful
with the code that you’re testing.

For example, we have a function that is making a request to the Twitter Server and if we do a test for that function, probably we’ll be doing real requests to the Twitter Servers. So, in this case, we have to be careful because, strictly, we don’t have to make real requests in our tests.

In the default case, when you run ./manage.py test, Django looks at the TEST RUNNER setting to determine what to do. By default, TEST RUNNER points to ‘django.test.runner.DiscoverRunner’. This class defines the default testing behavior. So, you can use any test framework that can be executed from Python code.

The interesting thing is that you can modify the test execution process. I mean, you can do ‘something’ before, during or after the test suite execution.

This is an example which show you how to override the Django DiscoverRunner Class and put your own TestRunner, but the trick is that you can do something before and after the execution. In this case, I mock the get requests then return
None value. So, if you’re testing a function which is doing requests to the Twitter Server, the function won’t be doing real requests to the Twitter Server.

The point is that you’re able to mock functions before the execution tests, of course, that depends of the circunstances because not everything should be mocked.