Lesson 17 - Test Driven Development
LEARN
Software Engineer
- Beyond programming
- Makes you more valuable
- Many will lack engineering skills
Test Driven Development
- Kent Beck - 2003
- Test-Driven Development
- High discipline process to produce outstanding quality
Test-Driven Development
- Select a feature
- Define a failing test
- Write enough code to pass the test
- Select the next feature and repeat
Shrinking World Software Planner
Shrinking World Software Planner
- Shrinking World Software Planner
- Test
- Fix
- Extend
- Improve
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
- List
- Detail
- Create
- Update
- Delete
Data CRUD Views Tests
- List
- Detail
- Add
- Edit
- Delete
View Test
- Page exists
- URL Route to view
- Template Used
- Objects in view
- HTML fragments
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
- Review To Do List
- Test Book CRUD
- Test Book Views
- Add fields to Book model
- Create Design Pattern
Project 6 Requirements
- Superhero Database with at least five superheros
- Superhero Data Model
- name, identity, description, strength, weakness, image
- Set default keyword argument in new fields added
- Superhero Custom Views
- List
- Detail
- Add
- Edit
- Delete
- Views Stylesheet
- View inheritance
- CSS