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?
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.
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.
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.
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.
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.
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__)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With