Skip to content

Commit 018475d

Browse files
committed
chapter 3 black
1 parent 3829576 commit 018475d

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

chapter_unit_test_first_view.asciidoc

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ Does that seem slightly redundant? Sometimes it can feel that way, but
126126
functional tests and unit tests do really have very different objectives, and
127127
they will usually end up looking quite different.
128128

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.
132132

133133
Enough theory for now—let's see how it looks in practice.
134134

@@ -174,8 +174,8 @@ silly failing test:
174174
----
175175
from django.test import TestCase
176176
177-
class SmokeTest(TestCase):
178177
178+
class SmokeTest(TestCase):
179179
def test_bad_maths(self):
180180
self.assertEqual(1 + 1, 3)
181181
----
@@ -274,13 +274,13 @@ test to something like this:
274274
----
275275
from django.urls import resolve
276276
from django.test import TestCase
277-
from lists.views import home_page #<2>
277+
from lists.views import home_page # <2>
278278
279-
class HomePageTest(TestCase):
280279
280+
class HomePageTest(TestCase):
281281
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>
284284
----
285285
====
286286

@@ -361,7 +361,7 @@ ERROR: test_root_url_resolves_to_home_page_view
361361
Traceback (most recent call last):
362362
File "...python-tdd-book/lists/tests.py", line 8, in
363363
test_root_url_resolves_to_home_page_view
364-
found = resolve('/')
364+
found = resolve("/")
365365
^^^^^^^^^^^^
366366
File ".../django/urls/base.py", line 24, in resolve
367367
return get_resolver(urlconf).resolve(path)
@@ -396,7 +396,7 @@ ERROR: test_root_url_resolves_to_home_page_view
396396
Traceback (most recent call last):
397397
File "...python-tdd-book/lists/tests.py", line 8, in
398398
test_root_url_resolves_to_home_page_view
399-
found = resolve('/') <3>
399+
found = resolve("/") <3>
400400
^^^^^^^^^^^^
401401
File ".../django/urls/base.py", line 24, in resolve
402402
return get_resolver(urlconf).resolve(path)
@@ -471,7 +471,7 @@ from django.contrib import admin
471471
from django.urls import path
472472
473473
urlpatterns = [
474-
path('admin/', admin.site.urls),
474+
path("admin/", admin.site.urls),
475475
]
476476
----
477477
====
@@ -513,7 +513,7 @@ from django.urls import path
513513
from lists import views
514514
515515
urlpatterns = [
516-
path('', views.home_page, name='home'),
516+
path("", views.home_page, name="home"),
517517
]
518518
----
519519
====
@@ -544,6 +544,7 @@ Back in 'lists/views.py':
544544
----
545545
from django.shortcuts import render
546546
547+
547548
# Create your views here.
548549
def home_page():
549550
pass
@@ -610,25 +611,23 @@ with HTML to the browser. Open up 'lists/tests.py', and add a new
610611
----
611612
from django.urls import resolve
612613
from django.test import TestCase
613-
from django.http import HttpRequest #<1>
614+
from django.http import HttpRequest # <1>
614615
615616
from lists.views import home_page
616617
617618
618619
class HomePageTest(TestCase):
619-
620620
def test_root_url_resolves_to_home_page_view(self):
621-
found = resolve('/')
621+
found = resolve("/")
622622
self.assertEqual(found.func, home_page)
623623
624-
625624
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>
632631
----
633632
====
634633
@@ -666,8 +665,10 @@ The Unit-Test/Code Cycle
666665
^^^^^^^^^^^^^^^^^^^^^^^^
667666
668667
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:
671672
672673
1. In the terminal, run the unit tests and see how they fail.
673674
2. In the editor, make a minimal code change to address the current test failure.
@@ -699,8 +700,10 @@ def home_page(request):
699700
* Tests:
700701
+
701702
----
702-
html = response.content.decode('utf8')
703+
html = response.content.decode("utf8")
704+
^^^^^^^^^^^^^^^^
703705
AttributeError: 'NoneType' object has no attribute 'content'
706+
704707
----
705708
706709
* Code--we use `django.http.HttpResponse`, as predicted:
@@ -712,6 +715,7 @@ AttributeError: 'NoneType' object has no attribute 'content'
712715
----
713716
from django.http import HttpResponse
714717
718+
715719
# Create your views here.
716720
def home_page(request):
717721
return HttpResponse()
@@ -721,7 +725,7 @@ def home_page(request):
721725
* Tests again:
722726
+
723727
----
724-
self.assertTrue(html.startswith('<html>'))
728+
self.assertTrue(html.startswith("<html>"))
725729
AssertionError: False is not true
726730
----
727731
@@ -734,7 +738,7 @@ AssertionError: False is not true
734738
[source,python]
735739
----
736740
def home_page(request):
737-
return HttpResponse('<html>')
741+
return HttpResponse("<html>")
738742
----
739743
====
740744
@@ -752,14 +756,14 @@ AssertionError: '<title>To-Do lists</title>' not found in '<html>'
752756
[source,python]
753757
----
754758
def home_page(request):
755-
return HttpResponse('<html><title>To-Do lists</title>')
759+
return HttpResponse("<html><title>To-Do lists</title>")
756760
----
757761
====
758762
759763
* Tests--almost there?
760764
+
761765
----
762-
self.assertTrue(html.endswith('</html>'))
766+
self.assertTrue(html.endswith("</html>"))
763767
AssertionError: False is not true
764768
----
765769
@@ -771,7 +775,7 @@ AssertionError: False is not true
771775
[source,python]
772776
----
773777
def home_page(request):
774-
return HttpResponse('<html><title>To-Do lists</title></html>')
778+
return HttpResponse("<html><title>To-Do lists</title></html>")
775779
----
776780
====
777781
@@ -807,7 +811,7 @@ FAIL: test_can_start_a_list_and_retrieve_it_later
807811
Traceback (most recent call last):
808812
File "...python-tdd-book/functional_tests.py", line 19, in
809813
test_can_start_a_list_and_retrieve_it_later
810-
self.fail('Finish the test!')
814+
self.fail("Finish the test!")
811815
AssertionError: Finish the test!
812816

813817
---------------------------------------------------------------------

0 commit comments

Comments
 (0)