Input / Output Tests
This is an example of how you can mock input values and test the output stream of student code.
The student must use the input()
function to prompt the user for their first name, the again for their last name. Then, print Hello, firstname lastname!
including the person's first and last name.
Sample Run:
It assumes the student's file name is example.py
. Replace the import with whatever you student's file name is.
Note: This code converts the output to all lowercase and removes spaces to compare against the correct value.
Grading Tests:
The io.StringIO()
for stdin
replaces the programs input stream. Each input must be separated by a newline character - \n
.
You will also notice that the assertion doesn't compare the output directly with what is expected. Instead it sees if the output stream .endswith()
the expected output. This is because the input()
function's prompt is included in the output stream, so we only want to check the last thing printed. If you want to include the prompts to ensure students properly included them, you can use assertEqual()
and and compare the entire output stream
Sample Solution:
Last updated