1.6 KiB
1.6 KiB
Chapter 6. Improving Functional Tests: Ensuring Isolation and Removing Voodoo Sleeps
6.1 Ensuring Test Isolation in Functional Tests
- Problem left on scratchpad: Clean up after FT runs
- Methods:
-
- In
functional_tests.py
add in codes to delete database
- In
-
- Use class
LiveServerTestCase
(Django 1.4). It will create a test db and start up a dev server for functional tests
- Use class
-
How to use LiveServerTestCase
:
- It's derived from Django's
TestCase
class, so can be run by Django test runner - Django test runner search for all scripts start with
test
What to do?:
- Create a
functional_test
directory, i.e. create a functional_test app - rename
functional_test.py
astests.py
and move it intofunctional_test
directory - Modify
tests.py
to let it useLiveServerTestCase
as shown below, and instead of hardcoding visit to localhost port 8000,LiveserverTestCase
provide attributelive_server_url
.
class NewVisitorTest(LiveServerTestCase):
...
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.live_server_url)
6.1.1 Running Just the Unit Tests
We can specify what we want to run by
python manage.py test functional_tests
or
python manage.py test lists
6.2 Aside: Upgrading Selenium and Geckodriver
Sometimes, mismatch between firefox and (Selenium + Geckodriver) may result failure in test, as firefox may auto upgrade itself at night. Upgrade Selenium + Geckodriver as follow:
pip install --upgrade selenium
- Download new geckodriver