Search code examples
pythondjangotesting

Get view used in Django tests


How do I find out, which view was called in Django tests?

In my test I want to ensure, that Middleware returns right view, is there any built in tool to do that?

Update

I want to do smth like:

from django.test import TestCase, Client
from my_app.views import my_view

c = Client()
response = c.get(url)
self.assertEqual(response.view, my_view)

So far, the only solution I see is using mock. I mean mocking my view and checking, whether it was called.


Solution

  • If I understand this question correctly, this is how I've been accomplishing this:

    found = resolve('url path')
    self.assertEqual(found.func, my_view)
    

    for class-based views:

    found = resolve('url path')
    self.assertEqual(found.func.__name__, my_view.as_view().__name__)
    

    Courtesy of Test-Driven Development with Python by Harry Percival.