if
cls
.
server_url
==
cls
.
live_server_url
:
super
()
.
tearDownClass
()
def
setUp
(
self
):
[
...
]
OK, when I said slightly hacking, I meant seriously hacking. Do you remember I said
that
LiveServerTestCase
had certain limitations? Well, one is that it always assumes
you want to use its own test server. I still want to be able to do that sometimes, but I also
want to be able to selectively tell it not to bother, and to use a real server instead.
setUpClass
is a similar method to
setUp
, also provided by
unittest
, which is
used to do test setup for the whole class—that means it only gets executed once,
rather than before every test method. This is where
LiveServerTestCase
/
Stat
icLiveServerCase
usually starts up its test server.
We look for the
liveserver
command-line argument (which is found in
sys.argv
).
If we find it, we tell our test class to skip the normal
setUpClass
, and just store
away our staging server URL in a variable called
server_url
instead.
This means we also need to change the three places we used to use
self.live_serv
er_url
:
functional_tests/tests.py (ch08l002).
def
test_can_start_a_list_and_retrieve_it_later
(
self
):
# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
self
.
browser
.
get
(
self
.
server_url
)
[
...
]
# Francis visits the home page. There is no sign of Edith's
# list
self
.
browser
.
get
(
self
.
server_url
)
[
...
]
def
test_layout_and_styling
(
self
):
# Edith goes to the home page
self
.
browser
.
get
(
self
.
server_url
)
We test that our little hack hasn’t broken anything by running the functional tests
“normally”:
$
python3 manage.py test functional_tests
[...]
Ran 2 tests in 8.544s
OK
And nowwe can try them against our staging server URL. I’mhosting my staging server
at
superlists-staging.ottg.eu
:
134
|
Chapter 8: Testing Deployment Using a Staging Site