This assignment is more about how to complete an assignment in this course than it is about Python programming. By the end of this assignment, you will know how to:
- run and debug code in the VS Code editor
- run python tests to verify your code is correct using pytest and the VS Code activity bar
- install assignment dependencies using
requirements.txt
- edit your reflection, requred as part of your assignment submission
- submit your assignment using git TODO ???
The each assignment will have a common layout.
code
folder contains our code / application. This is where you will create files and write code.code/solution
folder contains the solution to the assignment, for reference.tests
folder contains code to test our applicationrequirements.txt
contains the packages we need topip install
to execute the application codereadme.md
contains these instructions.vscode
folder contains VS Code setup configurations for running / debugging the application and tests.reflection.md
is where you submit your reflection, comments on what you learned, things that confuse you, etc.
Before starting this assignment you must:
Install the assignemnt python requirements:
- From VS Code, open a terminal: Menu => Terminal => New Terminal
- In the terminal, type and enter:
pip install -r requirements.txt
There is some code and tests already working in this assignment. These are sanity checks to ensure VS Code is configured properly.
- Open Testing in the activity bar: Menu => View => Testing
- Open the > by clicking on it next to assignment_01. Keep clicking on > until you see test_sould_pass in the test_assignment.py
- Click the Play button
|>
next to test_should_pass to execute the test. - A green check means the test code ran and the test has passed.
- A red X means the test code ran but the test has failed. When a test fails you will be given an error message and stack trace with line numbers.
For this part of the assignment you will learn how to read failed tests and debug them.
- Open
code/gpa.py
and read the instructions. The code has a couple of bugs. If you see them please do not fix. we will fix as part of the process. - Run the program: menu => Run => Run Without Debugging
In the terminal window, enter a gpa of3.3
. Based on the instructions, what should be the output? Does the program output correctly in this case? - Run it again and try
1.8
as input. Is the output correct? - Open the tests file
tests/test_assignment.py
look at the code underdef test_gpa():
. - Lines 10 through 18 list out 9 test cases. For example on line 17 when we input "4.0" the output should be "Summa Cum Laude".
- The code on lines 21-25 loop through each test case and run
./code/gpa.py
sending in the input gpa and scanning theprint()
output for the expected output.
The test code is correct, the code in gpa.py
is not. We will use the test code to find the error. This is the proper way to use test code. Assume you can write a test (you know what you expect the code to do, but you debug what is actually does)
- Click the red X to run the test. You will get an error. Observe the TEST RESULTS window. The last test before the
===FAILURES===
section is the failing test. - I'll add the test output here for convienence.
TEST 3: gpa.py INPUT: 1.8 EXPECT: Academic Probation ACTUAL: Enter GPA: for GPA 1.800 Result: Passing
For a1.8
input we expectAcademic Probation
but we gotPassing
. - This might be enough for you to figure out where the error is in the code under test,
gpa.py
. Let's assume its not, and thus we will use the debugger. - Open
gpa.py
. We are going to debug this program and find out why an input of1.8
does not result inAcademic Probation
- Place a breakpoint on line 19. Do this by clicking to the left of the line. A red dot will appear.
- Run the program with debugging. menu => Run => Start
- In the TERMINAL, enter a gpa of
1.8
- Program execution will pause on line 19. To the left of your program, you will see the variable
gpa
in the VARIABLES section. It has the value1.8
- To step through the code a line at a time press the F10 key or menu => Run => Step Over
- repeat the process until you are on line 26. Do you see the error? Compare the requirements on line 8 to the code on line 26.
- change line 26 accordinly
gpa <= 1.8
- stop execution menu => Run => Stop Debugging. NOTE: you cannot change code in the middle of execution
- Re-Test your change! Open the tests file
tests/test_assignment.py
and test the code the code underdef test_gpa():
. - It still doesn't pass all tests but it gets to TEST 7 now. repeat these steps to fix this error.
- Repeat these steps until all tests pass! NOTE: There are 3 bugs total in the code.
Write the program as per the instructions in numbers.py
and get the tests to pass in def test_numbers():
on line 27 of test_assignment.py