EECS 485 Lab 4: Server-side Dynamic Pages Tools
Due: 8:00pm EST February 14, 2020. Complete the lab quiz on Gradescope.
- 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.
Now let’s run through an example of how to use
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
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() ...
pdb features like
Try using the
p command to print a variable.
(Pdb++) p schema_sql ...
Run the test again, using the
--trace flag, which still start a debugger at the beginning of the test. Start
$ 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.
- 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
Complete the lab quiz by the due date.