COMP0034 Code to accompany the lecture covering Flask testing
- Create a venv
- Install the packages from requirements.txt (you may also need to add pytest)
OR Add the following packages yourself:
- Flask-SQLAlchemy
- Flask-Testing
- Flask-WTF
- Flask-Login
- pytest
- selenium
The latest release of Werkzeug appears to have caused issues for a number of packages.
If you install the latest Werkzeug rather than 16.0.1 then you may get an error ImportError: cannot import name 'cached_property' from 'werkzeug'
when you try to run tests that use Flask-Testing. To address this make an edit to External Libraries/site-packages/flask-testing/utils.py line 35
so that it reads from werkzeug.utils import cached_property
instead of from werkzeug import cached_property
.
This is documented here: jarus/flask-testing#143
- Open
test/backend_tests.py
- Run the unittest tests and check all run
- Add a new test in
class TestMain(BaseTestCase):
:- GIVEN a Flask application
- WHEN the ‘/view_profile' page is requested (GET) when the user is not logged in
- THEN the user is redirected to the login page and the message ‘You must be logged in to view that page.’ is displayed
- Open the
test/pytest_tests
folder - Run the pytest tests and check all run
- Add a new test in
test_main.py
:- GIVEN a Flask application
- WHEN the ‘/view_profile' page is requested (GET) when the user is not logged in
- THEN the user is redirected to the login page and the message ‘You must be logged in to view that page.’ is displayed
- Stop any running Flask app before starting the tests (as the Selenium tests run on the same port unless you change the config)
- Selenium should be installed, if not in the terminal for the venv of the project:
pip install selenium
- Download the browser driver https://sites.google.com/a/chromium.org/chromedriver/ (you MUST choose the version that matches your version of Chrome) and save it to the
test
directory of this project - Open
browser_tests.py
- Run the tests (3 tests, should all pass)
- Add a new test to test the login is successful for an existing user (try: email="[email protected]", password="cs1234567”)
- Install the Chrome or Firefox extension using the instructions on the Selenium site
- Click on the Selenium IDE plugin icon in Chrome and then open the
comp0034_testing.side
file from thetest
directory - Run the Flask app (e.g. in PyCharm)
- Run the
signup_test
- Try adding a new test to test the login process is successful (use the person signed up in the signup test)