Print Functions Tests
This is an example of how you can test the output stream of a student defined function.
This test makes sure the student's function named print_hello() prints "Hello World". 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:

1
import unittest
2
import sys, io
3
​
4
class CodingRoomsUnitTests(unittest.TestCase):
5
​
6
def test_default_case(self):
7
stdout = sys.stdout
8
sys.stdout = io.StringIO()
9
import example
10
example.print_hello()
11
output = sys.stdout.getvalue().strip("\n")
12
sys.stdout = stdout
13
answer = "Hello World".casefold().replace(" ", "")
14
student = output.casefold().replace(" ", "")
15
self.assertEqual(answer, student)
16
​
17
if __name__ == '__main__':
18
unittest.main()
Copied!

Sample Solution:

example.py
1
def print_hello():
2
print("Hello World")
Copied!
Last modified 2mo ago
Export as PDF
Copy link