import unittest import io import re from unittest import mock import math import student_X as main #Replace student_X with your filename import sys import io from io import StringIO class PrintWhetherDivisibleByThreeTests(unittest.TestCase): def test_default_case(self): function = main.print_whether_divisible_by_three try: docstring = function.__doc__ docstring = docstring.strip() except: docstring = "" correct = docstring != "" message = "docstring is blank." self.assertTrue(correct, message) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(10) output = fake_stdout.getvalue().strip() correct = "10" in output feedback = "Doesn't print the parameter in the output." self.assertTrue(correct, feedback) correct = "divisible" in output feedback = "The word 'divisible' is missing or misspelled in your output." self.assertTrue(correct, feedback) correct = output.endswith(".") feedback = "The output doesn't end with a period." self.assertTrue(correct, feedback) regex = "((N)|(n))o,? ?10 ?((isn't)|(is not)) divisible by 3 ?." correct = re.match(regex, output) feedback = "Doesn't print out the correct string when it's not divisible by 3." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(18) output = fake_stdout.getvalue().strip() regex = "((Y)|(y))es,? ?18 ?is divisible by 3 ?.?" correct = re.match(regex, output) feedback = "Doesn't print the correct output when the parameter is divisible by 3." self.assertTrue(correct, feedback) if __name__ == "__main__": tests = PrintWhetherDivisibleByThreeTests() tests.test_default_case()