@@ -126,9 +126,9 @@ Does that seem slightly redundant? Sometimes it can feel that way, but
126
126
functional tests and unit tests do really have very different objectives, and
127
127
they will usually end up looking quite different.
128
128
129
- NOTE: Functional tests should help you build an application with the right
130
- functionality, and guarantee you never accidentally break it. Unit tests
131
- should help you to write code that's clean and bug free.
129
+ NOTE: Functional tests should help you build an application with the right functionality,
130
+ and guarantee you never accidentally break it.
131
+ Unit tests should help you to write code that's clean and bug free.
132
132
133
133
Enough theory for now—let's see how it looks in practice.
134
134
@@ -174,8 +174,8 @@ silly failing test:
174
174
----
175
175
from django.test import TestCase
176
176
177
- class SmokeTest(TestCase):
178
177
178
+ class SmokeTest(TestCase):
179
179
def test_bad_maths(self):
180
180
self.assertEqual(1 + 1, 3)
181
181
----
@@ -274,13 +274,13 @@ test to something like this:
274
274
----
275
275
from django.urls import resolve
276
276
from django.test import TestCase
277
- from lists.views import home_page #<2>
277
+ from lists.views import home_page # <2>
278
278
279
- class HomePageTest(TestCase):
280
279
280
+ class HomePageTest(TestCase):
281
281
def test_root_url_resolves_to_home_page_view(self):
282
- found = resolve('/' ) #<1>
283
- self.assertEqual(found.func, home_page) #<1>
282
+ found = resolve("/" ) # <1>
283
+ self.assertEqual(found.func, home_page) # <1>
284
284
----
285
285
====
286
286
@@ -361,7 +361,7 @@ ERROR: test_root_url_resolves_to_home_page_view
361
361
Traceback (most recent call last):
362
362
File "... python-tdd-book/lists/tests.py", line 8, in
363
363
test_root_url_resolves_to_home_page_view
364
- found = resolve('/' )
364
+ found = resolve("/" )
365
365
^^^^^^^^^^^^
366
366
File "... /django/urls/base.py", line 24, in resolve
367
367
return get_resolver(urlconf).resolve(path)
@@ -396,7 +396,7 @@ ERROR: test_root_url_resolves_to_home_page_view
396
396
Traceback (most recent call last):
397
397
File "...python-tdd-book/lists/tests.py", line 8, in
398
398
test_root_url_resolves_to_home_page_view
399
- found = resolve('/' ) <3>
399
+ found = resolve("/" ) <3>
400
400
^^^^^^^^^^^^
401
401
File ".../django/urls/base.py", line 24, in resolve
402
402
return get_resolver(urlconf).resolve(path)
@@ -471,7 +471,7 @@ from django.contrib import admin
471
471
from django.urls import path
472
472
473
473
urlpatterns = [
474
- path(' admin/' , admin.site.urls),
474
+ path(" admin/" , admin.site.urls),
475
475
]
476
476
----
477
477
====
@@ -513,7 +513,7 @@ from django.urls import path
513
513
from lists import views
514
514
515
515
urlpatterns = [
516
- path('' , views.home_page, name=' home' ),
516
+ path("" , views.home_page, name=" home" ),
517
517
]
518
518
----
519
519
====
@@ -544,6 +544,7 @@ Back in 'lists/views.py':
544
544
----
545
545
from django.shortcuts import render
546
546
547
+
547
548
# Create your views here.
548
549
def home_page():
549
550
pass
@@ -610,25 +611,23 @@ with HTML to the browser. Open up 'lists/tests.py', and add a new
610
611
----
611
612
from django.urls import resolve
612
613
from django.test import TestCase
613
- from django.http import HttpRequest # <1>
614
+ from django.http import HttpRequest # <1>
614
615
615
616
from lists.views import home_page
616
617
617
618
618
619
class HomePageTest(TestCase):
619
-
620
620
def test_root_url_resolves_to_home_page_view(self):
621
- found = resolve('/' )
621
+ found = resolve("/" )
622
622
self.assertEqual(found.func, home_page)
623
623
624
-
625
624
def test_home_page_returns_correct_html(self):
626
- request = HttpRequest() #<1>
627
- response = home_page(request) #<2>
628
- html = response.content.decode(' utf8' ) #<3>
629
- self.assertTrue(html.startswith(' <html>' )) #<4>
630
- self.assertIn(' <title>To-Do lists</title>' , html) #<5>
631
- self.assertTrue(html.endswith(' </html>' )) #<4>
625
+ request = HttpRequest() # <1>
626
+ response = home_page(request) # <2>
627
+ html = response.content.decode(" utf8" ) # <3>
628
+ self.assertTrue(html.startswith(" <html>" )) # <4>
629
+ self.assertIn(" <title>To-Do lists</title>" , html) # <5>
630
+ self.assertTrue(html.endswith(" </html>" )) # <4>
632
631
----
633
632
====
634
633
@@ -666,8 +665,10 @@ The Unit-Test/Code Cycle
666
665
^^^^^^^^^^^^^^^^^^^^^^^^
667
666
668
667
669
- ((("unit tests", "in Django", "unit-test/code cycle", secondary-sortas="Django")))((("unit-test/code cycle")))((("Test-Driven Development (TDD)", "concepts", "unit-test/code cycle")))We
670
- can start to settle into the TDD 'unit-test/code cycle' now:
668
+ ((("unit tests", "in Django", "unit-test/code cycle", secondary-sortas="Django")))
669
+ ((("unit-test/code cycle")))
670
+ ((("Test-Driven Development (TDD)", "concepts", "unit-test/code cycle")))
671
+ We can start to settle into the TDD 'unit-test/code cycle' now:
671
672
672
673
1. In the terminal, run the unit tests and see how they fail.
673
674
2. In the editor, make a minimal code change to address the current test failure.
@@ -699,8 +700,10 @@ def home_page(request):
699
700
* Tests:
700
701
+
701
702
----
702
- html = response.content.decode('utf8')
703
+ html = response.content.decode("utf8")
704
+ ^^^^^^^^^^^^^^^^
703
705
AttributeError: 'NoneType' object has no attribute 'content'
706
+
704
707
----
705
708
706
709
* Code--we use `django.http.HttpResponse`, as predicted:
@@ -712,6 +715,7 @@ AttributeError: 'NoneType' object has no attribute 'content'
712
715
----
713
716
from django.http import HttpResponse
714
717
718
+
715
719
# Create your views here.
716
720
def home_page(request):
717
721
return HttpResponse()
@@ -721,7 +725,7 @@ def home_page(request):
721
725
* Tests again:
722
726
+
723
727
----
724
- self.assertTrue(html.startswith(' <html>' ))
728
+ self.assertTrue(html.startswith(" <html>" ))
725
729
AssertionError: False is not true
726
730
----
727
731
@@ -734,7 +738,7 @@ AssertionError: False is not true
734
738
[source,python]
735
739
----
736
740
def home_page(request):
737
- return HttpResponse(' <html>' )
741
+ return HttpResponse(" <html>" )
738
742
----
739
743
====
740
744
@@ -752,14 +756,14 @@ AssertionError: '<title>To-Do lists</title>' not found in '<html>'
752
756
[source,python]
753
757
----
754
758
def home_page(request):
755
- return HttpResponse(' <html><title>To-Do lists</title>' )
759
+ return HttpResponse(" <html><title>To-Do lists</title>" )
756
760
----
757
761
====
758
762
759
763
* Tests--almost there?
760
764
+
761
765
----
762
- self.assertTrue(html.endswith(' </html>' ))
766
+ self.assertTrue(html.endswith(" </html>" ))
763
767
AssertionError: False is not true
764
768
----
765
769
@@ -771,7 +775,7 @@ AssertionError: False is not true
771
775
[source,python]
772
776
----
773
777
def home_page(request):
774
- return HttpResponse(' <html><title>To-Do lists</title></html>' )
778
+ return HttpResponse(" <html><title>To-Do lists</title></html>" )
775
779
----
776
780
====
777
781
@@ -807,7 +811,7 @@ FAIL: test_can_start_a_list_and_retrieve_it_later
807
811
Traceback (most recent call last):
808
812
File "...python-tdd-book/functional_tests.py", line 19, in
809
813
test_can_start_a_list_and_retrieve_it_later
810
- self.fail(' Finish the test!' )
814
+ self.fail(" Finish the test!" )
811
815
AssertionError: Finish the test!
812
816
813
817
---------------------------------------------------------------------
0 commit comments