Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to test "render to template" functions in django? (TDD)

How should I test these functions? All they do is render the html page and pass some objects to the html page.

def index(request):
    companies = Company.objects.filter(approved = True);
    return direct_to_template(request, 'home.html', {'companies': companies} );
like image 463
iCodeLikeImDrunk Avatar asked May 15 '12 20:05

iCodeLikeImDrunk


1 Answers

One could test the following:

  1. Response code
  2. Template used
  3. Template contains some specific text

The code would look something like this:

class TestPage(TestCase):

   def setUp(self):
       self.client = Client()

   def test_index_page(self):
       url = reverse('index')
       response = self.client.get(url)
       self.assertEqual(response.status_code, 200)
       self.assertTemplateUsed(response, 'index.html')
       self.assertContains(response, 'Company Name XYZ')
like image 156
super9 Avatar answered Sep 20 '22 15:09

super9