eyeinthebrick
eyeinthebrick

Reputation: 548

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.

Upvotes: 1

Views: 330

Answers (1)

Adam Starrh
Adam Starrh

Reputation: 6958

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.

Upvotes: 4

Related Questions