Skip to content

Commit 768d2e6

Browse files
authored
Merge pull request #133 from hjwp/python3.11-django-4
Python3.11 & Django 4
2 parents a7413f8 + 36e9137 commit 768d2e6

33 files changed

+1489
-1307
lines changed

.github/workflows/tests.yml

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Book tests
33
on: [push]
44

55
jobs:
6-
build:
6+
tests:
77

88
runs-on: ubuntu-latest
99

@@ -26,10 +26,10 @@ jobs:
2626
git config --global user.email "[email protected]"
2727
git config --global user.name "Elspeth See-Eye"
2828
29-
- name: Set up Python 3.8
29+
- name: Set up Python 3.11
3030
uses: actions/setup-python@v1
3131
with:
32-
python-version: 3.8
32+
python-version: 3.11
3333

3434
- name: Install apt stuff and other dependencies
3535
run: |
@@ -53,14 +53,40 @@ jobs:
5353
env
5454
5555
- name: Test chapter 1
56+
if: always()
5657
run: |
5758
make test_chapter_01
5859
5960
- name: Test chapter 2
61+
if: always()
6062
run: |
6163
make test_chapter_02_unittest
6264
63-
- name: Test all chapters in parallel, yolo
65+
- name: Test chapter 3
66+
if: always()
6467
run: |
65-
make build
66-
pytest --tb=short --color=yes --numprocesses=4 tests/test_chapter_*
68+
make test_chapter_unit_test_first_view
69+
70+
- name: Test chapter 4
71+
if: always()
72+
run: |
73+
make test_chapter_philosophy_and_refactoring
74+
75+
- name: Test chapter 5
76+
if: always()
77+
run: |
78+
make test_chapter_post_and_database
79+
80+
- name: Test chapter 6
81+
if: always()
82+
run: |
83+
make test_chapter_explicit_waits_1
84+
85+
- name: Test chapter 7
86+
if: always()
87+
run: |
88+
make test_chapter_working_incrementally
89+
# - name: Test all chapters in parallel, yolo
90+
# run: |
91+
# make build
92+
# pytest --tb=short --color=yes --numprocesses=4 tests/test_chapter_*

Makefile

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
SOURCES := $(wildcard *.asciidoc)
22
HTML_PAGES := $(patsubst %.asciidoc, %.html, ${SOURCES})
3-
RUN_ASCIIDOCTOR = asciidoctor -a source-highlighter=coderay -a stylesheet=asciidoctor.css -a linkcss -a icons=font -a compat-mode -a '!example-caption' -a last-update-label='License: Creative Commons <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode">CC-BY-NC-ND</a>. Last updated:'
3+
TESTS := $(patsubst %.asciidoc, test_%, ${SOURCES})
4+
5+
RUN_ASCIIDOCTOR = asciidoctor -a source-highlighter=coderay -a stylesheet=asciidoctor.css -a linkcss -a icons=font -a compat-mode -a '!example-caption' -a last-update-label='License: Creative Commons <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode">CC-BY-NC-ND</a>. Last updated:'
46
RUN_OREILLY_FLAVOURED_ASCIIDOCTOR = ./asciidoc/asciidoctor/bin/asciidoctor -v --trace -d book --safe -b htmlbook --template-dir ./asciidoc/asciidoctor-htmlbook/htmlbook
57

8+
export PYTHONHASHSEED = 0
9+
export PYTHONDONTWRITEBYTECODE = 1
10+
export MOZ_HEADLESS = 1
11+
export TMPDIR := $(HOME)/snap/firefox/common/tmp
12+
13+
$(TMPDIR):
14+
make -p $(TMPDIR)
15+
616
book.html: $(SOURCES)
717

8-
build: $(HTML_PAGES)
18+
build: $(HTML_PAGES) $(TMPDIR)
919

1020
test: build
1121
git submodule init
12-
python3 update_source_repo.py
13-
PYTHONHASHSEED=0 PYTHONDONTWRITEBYTECODE=1 \
22+
python tests/update_source_repo.py
1423
./run_all_tests.sh
1524

25+
testall: build
26+
pytest --tb=short --color=yes --numprocesses=auto tests/test_chapter_*
27+
28+
testall4: build
29+
pytest --tb=short --color=yes --numprocesses=4 tests/test_chapter_*
30+
1631
%.html: %.asciidoc
1732
$(RUN_ASCIIDOCTOR) $<
1833

@@ -21,12 +36,10 @@ oreilly.%.asciidoc: %.asciidoc
2136

2237

2338
test_%: %.html
24-
PYTHONHASHSEED=0 PYTHONDONTWRITEBYTECODE=1 MOZ_HEADLESS=1 \
25-
py.test -s --tb=short ./tests/$@.py
39+
pytest -s --tb=short ./tests/$@.py
2640

2741
silent_test_%: %.html
28-
python3 update_source_repo.py $(subst silent_test_chapter_,,$@)
29-
PYTHONHASHSEED=0 PYTHONDONTWRITEBYTECODE=1 MOZ_HEADLESS=1 \
42+
python tests/update_source_repo.py $(subst silent_test_chapter_,,$@)
3043
py.test --tb=short ./tests/$(subst silent_,,$@).py
3144

3245
clean:

appendix_bdd.asciidoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ formatting syntax:
396396
@when('I create a list with first item "{first_item_text}"')
397397
def create_a_list(context, first_item_text):
398398
context.browser.get(context.get_url('/'))
399-
context.browser.find_element_by_id('id_text').send_keys(first_item_text)
400-
context.browser.find_element_by_id('id_text').send_keys(Keys.ENTER)
399+
context.browser.find_element(By.ID, 'id_text').send_keys(first_item_text)
400+
context.browser.find_element(By.ID, 'id_text').send_keys(Keys.ENTER)
401401
wait_for_list_item(context, first_item_text)
402402
----
403403
====
@@ -445,8 +445,8 @@ from selenium.webdriver.common.keys import Keys
445445
446446
@when('I add an item "{item_text}"')
447447
def add_an_item(context, item_text):
448-
context.browser.find_element_by_id('id_text').send_keys(item_text)
449-
context.browser.find_element_by_id('id_text').send_keys(Keys.ENTER)
448+
context.browser.find_element(By.ID, 'id_text').send_keys(item_text)
449+
context.browser.find_element(By.ID, 'id_text').send_keys(Keys.ENTER)
450450
wait_for_list_item(context, item_text)
451451
452452
@@ -707,7 +707,7 @@ class ListPage(object):
707707
708708
709709
def get_item_input_box(self):
710-
return self.test.browser.find_element_by_id('id_text')
710+
return self.test.browser.find_element(By.ID, 'id_text')
711711
----
712712
====
713713

chapter_01.asciidoc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ making a few little goat noises as you do it, it may help:
6666
from selenium import webdriver
6767
6868
browser = webdriver.Firefox()
69-
browser.get('http://localhost:8000')
69+
browser.get("http://localhost:8000")
7070
71-
assert 'Django' in browser.title
71+
assert "Django" in browser.title
7272
----
7373
====
7474

@@ -93,13 +93,13 @@ Let's try running it:
9393
----
9494
$ pass:quotes[*python functional_tests.py*]
9595
Traceback (most recent call last):
96-
File "functional_tests.py", line 4, in <module>
97-
browser.get('http://localhost:8000')
98-
File ".../selenium/webdriver/remote/webdriver.py", line 333, in get
99-
self.execute(Command.GET, {'url': url})
100-
File ".../selenium/webdriver/remote/webdriver.py", line 321, in execute
96+
File "...python-tdd-book/functional_tests.py", line 4, in <module>
97+
browser.get("http://localhost:8000")
98+
File ".../selenium/webdriver/remote/webdriver.py", line 449, in get
99+
self.execute(Command.GET, {"url": url})
100+
File ".../selenium/webdriver/remote/webdriver.py", line 440, in execute
101101
self.error_handler.check_response(response)
102-
File ".../selenium/webdriver/remote/errorhandler.py", line 242, in
102+
File ".../selenium/webdriver/remote/errorhandler.py", line 245, in
103103
check_response
104104
raise exception_class(message, screen, stacktrace)
105105
selenium.common.exceptions.WebDriverException: Message: Reached error page: abo
@@ -335,10 +335,10 @@ Changes to be committed:
335335
new file: functional_tests.py
336336
new file: manage.py
337337
new file: superlists/__init__.py
338-
new file: superlists/__pycache__/__init__.cpython-37.pyc
339-
new file: superlists/__pycache__/settings.cpython-37.pyc
340-
new file: superlists/__pycache__/urls.cpython-37.pyc
341-
new file: superlists/__pycache__/wsgi.cpython-37.pyc
338+
new file: superlists/__pycache__/__init__.cpython-311.pyc
339+
new file: superlists/__pycache__/settings.cpython-311.pyc
340+
new file: superlists/__pycache__/urls.cpython-311.pyc
341+
new file: superlists/__pycache__/wsgi.cpython-311.pyc
342342
new file: superlists/settings.py
343343
new file: superlists/urls.py
344344
new file: superlists/wsgi.py
@@ -352,10 +352,10 @@ commit those. Let's remove them from Git and add them to
352352
[subs="specialcharacters,macros"]
353353
----
354354
$ pass:[<strong>git rm -r --cached superlists/__pycache__</strong>]
355-
rm 'superlists/__pycache__/__init__.cpython-37.pyc'
356-
rm 'superlists/__pycache__/settings.cpython-37.pyc'
357-
rm 'superlists/__pycache__/urls.cpython-37.pyc'
358-
rm 'superlists/__pycache__/wsgi.cpython-37.pyc'
355+
rm 'superlists/__pycache__/__init__.cpython-311.pyc'
356+
rm 'superlists/__pycache__/settings.cpython-311.pyc'
357+
rm 'superlists/__pycache__/urls.cpython-311.pyc'
358+
rm 'superlists/__pycache__/wsgi.cpython-311.pyc'
359359
$ pass:[<strong>echo "__pycache__" >> .gitignore</strong>]
360360
$ pass:[<strong>echo "*.pyc" >> .gitignore</strong>]
361361
----

chapter_02_unittest.asciidoc

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ browser = webdriver.Firefox()
7878
7979
# Edith has heard about a cool new online to-do app. She goes
8080
# to check out its homepage
81-
browser.get('http://localhost:8000')
81+
browser.get("http://localhost:8000")
8282
8383
# She notices the page title and header mention to-do lists
84-
assert 'To-Do' in browser.title
84+
assert "To-Do" in browser.title
8585
8686
# She is invited to enter a to-do item straight away
8787
@@ -164,8 +164,8 @@ And then, in another shell, run the tests:
164164
----
165165
$ pass:quotes[*python functional_tests.py*]
166166
Traceback (most recent call last):
167-
File "functional_tests.py", line 10, in <module>
168-
assert 'To-Do' in browser.title
167+
File "...python-tdd-book/functional_tests.py", line 10, in <module>
168+
assert "To-Do" in browser.title
169169
AssertionError
170170
----
171171
@@ -193,7 +193,7 @@ something like:
193193
[role="skipme"]
194194
[source,python]
195195
----
196-
assert 'To-Do' in browser.title, "Browser title was " + browser.title
196+
assert "To-Do" in browser.title, "Browser title was " + browser.title
197197
----
198198

199199
And we could also use a `try/finally` to clean up the old Firefox window. But
@@ -209,27 +209,28 @@ use that! In [keep-together]#_functional_tests.py_#:
209209
from selenium import webdriver
210210
import unittest
211211
212-
class NewVisitorTest(unittest.TestCase): #<1>
213212
214-
def setUp(self): #<3>
213+
class NewVisitorTest(unittest.TestCase): # <1>
214+
def setUp(self): # <3>
215215
self.browser = webdriver.Firefox()
216216
217-
def tearDown(self): #<3>
217+
def tearDown(self): # <3>
218218
self.browser.quit()
219219
220-
def test_can_start_a_list_and_retrieve_it_later(self): #<2>
220+
def test_can_start_a_list_and_retrieve_it_later(self): # <2>
221221
# Edith has heard about a cool new online to-do app. She goes
222222
# to check out its homepage
223-
self.browser.get('http://localhost:8000')
223+
self.browser.get("http://localhost:8000")
224224
225225
# She notices the page title and header mention to-do lists
226-
self.assertIn('To-Do', self.browser.title) #<4>
227-
self.fail('Finish the test!') #<5>
226+
self.assertIn("To-Do", self.browser.title) # <4>
227+
self.fail("Finish the test!") # <5>
228228
229229
# She is invited to enter a to-do item straight away
230230
[...rest of comments as before]
231231
232-
if __name__ == '__main__': #<6>
232+
233+
if __name__ == "__main__": # <6>
233234
unittest.main()
234235
----
235236
====
@@ -283,13 +284,15 @@ Let's try it!
283284
$ pass:quotes[*python functional_tests.py*]
284285
F
285286
======================================================================
286-
FAIL: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest)
287+
FAIL: test_can_start_a_list_and_retrieve_it_later
288+
(__main__.NewVisitorTest.test_can_start_a_list_and_retrieve_it_later)
287289
---------------------------------------------------------------------
288290
Traceback (most recent call last):
289-
File "functional_tests.py", line 18, in
291+
File "...python-tdd-book/functional_tests.py", line 18, in
290292
test_can_start_a_list_and_retrieve_it_later
291-
self.assertIn('To-Do', self.browser.title)
292-
AssertionError: 'To-Do' not found in 'Welcome to Django'
293+
self.assertIn("To-Do", self.browser.title)
294+
AssertionError: 'To-Do' not found in 'The install worked successfully!
295+
Congratulations!'
293296
294297
---------------------------------------------------------------------
295298
Ran 1 test in 1.747s
@@ -333,10 +336,10 @@ index d333591..b0f22dc 100644
333336
+import unittest
334337
335338
-browser = webdriver.Firefox()
336-
-browser.get('http://localhost:8000')
339+
-browser.get("http://localhost:8000")
337340
+class NewVisitorTest(unittest.TestCase):
338341
339-
-assert 'Django' in browser.title
342+
-assert "Django" in browser.title
340343
+ def setUp(self):
341344
+ self.browser = webdriver.Firefox()
342345
+

0 commit comments

Comments
 (0)