📋
Auto-Grading for Teachers
  • Learn How to Auto-Grade
  • Assertions and Unit Testing
  • Request / Submit Examples
  • 🐍Python Examples
    • Check Variable Values
    • Output Tests
    • Input / Output Tests
    • Print Function Tests
    • Return Function Tests
    • Random Number Tests
    • Parse Student Code
  • ☕Java Examples
    • Check Variable Values
    • Output Tests
    • Input / Output Tests
    • Return Method Tests
    • Class - .toString() and Void Method Tests
    • Random Numbers Tests
    • Array and ArrayList Tests
    • Parse Student Code
  • 💎Ruby Examples
    • Output Tests
    • Input / Output Tests
    • Print Function Tests
    • Return Function Tests
    • Parse Student Code
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Python Examples

Print Function 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:

import unittest
import sys, io

class CodingRoomsUnitTests(unittest.TestCase):

    def test_default_case(self):
        stdout = sys.stdout
        sys.stdout = io.StringIO()
        import example
        example.print_hello()
        output = sys.stdout.getvalue().strip("\n")
        sys.stdout = stdout
        answer = "Hello World".casefold().replace(" ", "")
        student = output.casefold().replace(" ", "")
        self.assertEqual(answer, student)

if __name__ == '__main__':
    unittest.main()

Sample Solution:

example.py
def print_hello():
    print("Hello World")
PreviousInput / Output TestsNextReturn Function Tests

Last updated 3 years ago

Was this helpful?

🐍