[Django]-Monkeypatching views with Django's LiveServerTestCase

5👍

You are monkey patching the view function in the view module. Any location that has already imported that view (a reference to the function) will still hold the reference to the old (real) view function.

Django’s urlconf mechanism import and configures itself with the real view, on the first request (which probably happends in another test case).

When you change the function in your views module, the urlconf will not notice it, since it already holds a reference to the old view function. Monkey patching in Python changes names/references, not functions themselves.

You are using the pytest’s monkeypatch helper, but this guide in the mock library documentation provides some good info about where to apply monkey patches:

http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch

In this particular case, I think your best bet is to patch the Sorl-call with static test data, rather than the view. Since you are doing a Selenium test, I think it would be very good to keep the real view. What are you actually testing if you replace the entire view?

If the view itself contains a lot of Sorl-specific code, you might want to break that code out into a separate function, which you then can easily patch out.

If you really want to change the view, I suggest you override the urlconf to point at your new view:
https://pytest-django.readthedocs.org/en/latest/helpers.html#pytest-mark-urls-override-the-urlconf

Leave a comment