Output Tests
This is an example of how you can test the output stream of student code.
This test makes sure the student's code 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
stdout = sys.stdout
5
​
6
class CodingRoomsUnitTests(unittest.TestCase):
7
​
8
def setUp(self):
9
global stdout
10
stdout = sys.stdout
11
sys.stdout = io.StringIO()
12
​
13
def tearDown(self):
14
global stdout
15
sys.stdout = stdout
16
17
def test_default_case(self):
18
import example
19
20
output = sys.stdout.getvalue().strip("\n")
21
​
22
answer = "Hello World".casefold().replace(" ", "")
23
student = output.casefold().replace(" ", "")
24
25
self.assertEqual(answer, student)
26
​
27
if __name__ == '__main__':
28
unittest.main()
Copied!

Sample Solution:

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