Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django how to assert url pattern resolves to correct class based view function

I have a class based view

class HomePage(View):
   def get(self, request):
       return HttpResponse('<p>This is content.</p>')

and url-pattern defined as below:

urlpatterns = patterns('',
                  url(r'^$', HomePage.as_view()),
              )

To this pattern resolves to current view function, I wrote a test like this:

class HomePageTest(TestCase):

def test_root_url_resolves_to_home_page_view(self):
    found = resolve('/')
    self.assertIsInstance(found.func, HomePage)

By running this unittest I am getting following error:

self.assertIsInstance(found.func, HomePage)
AssertionError: <function HomePage at 0x7f85dd2c7840> is not an instance of <class 'web.views.HomePage'>

Any Idea how to test this case?

like image 892
surenyonjan Avatar asked Dec 05 '14 16:12

surenyonjan


People also ask

Which method is used instead of PATH () in URLs py to pass in regular expressions as routes?

To do so, use re_path() instead of path() . In Python regular expressions, the syntax for named regular expression groups is (?P<name>pattern) , where name is the name of the group and pattern is some pattern to match.

How do I change the default URL in Django?

Set up app folder's urls.py and html files In the same directory, you should have a file named views.py. We will create a function called index which is what makes the http request for our website to be loaded. Now, we've set it up such that http://127.0.0.1:8000/homepage will render the HTML template index.

What is Urlconf in Django?

A URLconf is similar to a table of contents for our Django-powered web site. It's a mapping between URL patterns and the view functions that need to be called for those URLs. First, we will have to import the view function that we want our server to run when the URL is matched.

What is resolve in Django?

The resolve() function can be used for resolving URL paths to the corresponding view functions. It has the following signature: resolve (path, urlconf=None) path is the URL path you want to resolve. As with reverse() , you don't need to worry about the urlconf parameter.


3 Answers

Django's View.as_view() creates a function with a view_class attribute which points to the class-based view. So use:

self.assertEquals(found.func.view_class, HomePage)

Avoids the problem of two class-based views in different modules with the same name.

like image 94
J. Freedman Avatar answered Oct 28 '22 22:10

J. Freedman


May be it's an old question, but in django>=1.8 assertion like

self.assertEquals(found.func.func_name, HomePage.__name__)

AttributeError: 'function' object has no attribute 'func_name' so I changed it to

self.assertEqual(found.func.__name__, HomePage.__name__)
like image 4
valignatev Avatar answered Oct 28 '22 22:10

valignatev


Resolve will return the function that is returned when calling HomePage.as_view(), and not an object of that type. However, from a quick test there may be a way that you could write this test:

self.assertEquals(found.func.func_name, HomePage.__name__)

Note that here we specify HomePage.__name__ instead of 'HomePage' because this will get picked up if the name of the class is changed using refactoring tools.

The downside of this is, should you have wired up a view class with the same name but from a different module, this unit test would not fail. Of course this is more of a risk with a generic view class name such as HomePage but should be less of a risk with other view classes.

like image 2
robjohncox Avatar answered Oct 28 '22 21:10

robjohncox