EECS 485 Lab
Lab 4: Server-side Dynamic Pages Tools
Goals
- Be able to use the browser tools to inspect cookies
- Improve your ability to debug Python/Flask code
Browser Development Tools
Review browser tools, you’ll still need those.
Complete the two new sections in the tutorial on Cookies and Private Browsing. Do not complete the JavaScript debugger section, we’ll cover that in the future.
Python tools
First, refresh yourself on pdb++ and pytest.
Now let’s run through an example of how to use pdb
and pytest
together.
Make sure your virtual environment is activated and you have Pdb++ installed in your virtual environment.
$ echo $VIRTUAL_ENV
/Users/awdeorio/src/eecs485/p2-insta485-serverside/env
$ pip install pdbpp
For the purpose of this tutorial, we’ll add a failure to one of the project 2 tests. Edit sql/schema.sql
and make an intentional typo. Change username
to uuusername
.
CREATE TABLE users(
uuusername VARCHAR(20) NOT NULL,
...
Run the database tests. They should fail.
$ pytest -v tests/test_database_public.py
...
============================== 2 failed in 0.12s ===============================
Run the tests again, but with pytest’s -x
flag, which will stop after the first failure.
$ pytest -v tests/test_database_public.py -x
...
============================== 1 failed in 0.11s ===============================
Run the test again, adding pytest’s --pdb
flag, which still start a debugger at the line of the failure.
$ pytest -v tests/test_database_public.py -x --pdb
...
(Pdb++)
Start Pdb++’s sticky mode, which will show you the function where the exception occurred and also highlights the line that threw the error.
(Pdb++) sticky
10 def test_sql_schema(db_connection):
11 """Verify schema.sql produces correct tables.
12
13 Note: 'db_connection' is a fixture fuction that provides an empty,
14 in-memory sqlite3 database. It is implemented in conftest.py and r
15 many tests. Docs: https://docs.pytest.org/en/latest/fixture.html
16 """
17 # Load student schema.sql
18 with open("sql/schema.sql") as infile:
19 schema_sql = infile.read()
20 assert "PRAGMA foreign_keys = ON" in schema_sql
21 -> db_connection.executescript(schema_sql)
22 db_connection.commit()
...
Now, use pdb
features like continue
, print
, and more to debug why your code is failing the tests.
Try using the p
command to print a variable.
(Pdb++) p schema_sql
...
Quit Pdb++
.
(Pdb++) q
Run the test again, using the --trace
flag, which still start a debugger at the beginning of the test. Start sticky
mode.
$ pytest -v tests/test_database_public.py -x --trace
...
(Pdb++) sticky
Step through the test line-by-line until you reach the failure. Next line is n
in Pdb. Pro-tip: just keep hitting enter after entering a single n
. The default is to repeat the previous command.
(Pdb++) n
(Pdb++) n
(Pdb++) n
...
Now that you’ve practiced with some of the features of pytest, don’t forget to fix the SQL typo we inserted.
Completion Criteria
- Learn how to use browser development tools
- Learn how to inspect cookies in the browser and use private browsing
- Review how to debug Python code using Pdb
Lab Quiz
Complete the lab quiz by the due date.