Skip to content

Instantly share code, notes, and snippets.

@aipi
Created June 28, 2017 15:25
Show Gist options
  • Save aipi/8a5933fd53cc8e7765cad9505ec19106 to your computer and use it in GitHub Desktop.
Save aipi/8a5933fd53cc8e7765cad9505ec19106 to your computer and use it in GitHub Desktop.
TDD Django unittest to check the type of a context, like Queryset, List Class, so on.
from django.test import TestCase
# Is necessary import <class 'django.db.models.query.QuerySet'> to make this validation
from django.db.models.query import QuerySet
class UserTestCase(TestCase):
def setUp(self)
# (your setUp code here...)
self.create_models()
def create_models(self):
# (your models code here...)
def test_ix_view(self):
# (some tests case here ...)
# To validated if context is equal QuerySet, compare if the type of context is equal Django QuerySet Class.
# type(self.response.context['queryset_context']) == <class 'django.db.models.query.QuerySet'>
self.assertIs(type(self.response.context['queryset_context']), QuerySet) # This will return True
self.assertEqual(len(self.response.context['queryset_context']), 2)
# The same idea can be used to another types, like it:
# type(self.response.context['list_context']) == <class 'list'>
self.assertIs(type(self.response.context['list_context']), list) # This will return True
self.assertEqual(len(self.response.context['list_context']), 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment