UNC BACS 350

Web Apps with Python/Django

Logo

Lesson 17 - Test Driven Development

LEARN

Software Engineer

Test Driven Development

Test-Driven Development

Shrinking World Software Planner

Shrinking World Software Planner

Basic Automatic Test

class MyTest(TestCase):

    def test_one_thing(self):
        result = do_an_action()
        self.assertEqual(result, "Expected Answer")

CRUD Tests

class BookTest(TestCase):

    def test_book_list(self):
        self.assertEqual(len(Book.objects.all()), 0)

    def test_add_book(self):
        Book.objects.create(title='Tale of 2 Cities', author='Chuck Dickens')
        Book.objects.create(title='Iliad', author='Homer')
        self.assertEqual(len(Book.objects.all()), 2)

CRUD Tests

Data CRUD Views Tests

View Test

Test Page Exists

python def test_page_exists(self): response = self.client.get('/book/') self.assertEqual(response.status_code, 200)

Test URL Route to view

python def test_book_list_view(self): url = reverse('book_list') response = self.client.get(url) self.assertEqual(response.status_code, 200)

Template Used

python def test_book_list_view(self): response = self.client.get('/book/') self.assertTemplateUsed(response, 'book_list.html') self.assertTemplateUsed(response, 'book_theme.html')

Objects in view

python def test_objects_in_view(self): response = self.client.get('/book/') self.assertContains(response, '<li>', count=3)

HTML fragments

python def test_contains(self): self.assertContains(response, '<title>Book List</title>')


BUILD

Demo Code for Book Builder

Live Demo

Project 6 Requirements