import unittest import io import re from unittest import mock import math import student_X as main #Replace student_X with your filename class PrintMonkeyMountainTests(unittest.TestCase): def test_default_case(self): function = main.print_monkey_mountain test = False try: incorrect = "" result = function.__doc__.strip() test = incorrect != result except: pass self.assertTrue(test, "No docstring.") inputs = (2) test = False with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: if isinstance(inputs, tuple): result = function(*inputs) else: result = function(inputs) printed_output = fake_stdout.getvalue().strip() lines = printed_output.split("\n") correct = len(lines) == 2 self.assertTrue(correct, "Doesn't print the correct number of lines.") correct = 'monkey\nmonkeymonkey' test = correct == printed_output self.assertTrue(test, "Doesn't correctly print the mountain of monkeys when the parameter is 2.") inputs = (3) test = False with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: if isinstance(inputs, tuple): result = function(*inputs) else: result = function(inputs) printed_output = fake_stdout.getvalue().strip() lines = printed_output.split("\n") correct = len(lines) == 3 self.assertTrue(correct, "Doesn't print the correct number of lines.") correct = 'monkey\nmonkeymonkey\nmonkeymonkeymonkey' test = correct == printed_output self.assertTrue(test, "Doesn't correctly print the mountain of monkeys when the parameter is 3. (Poor monkeys!)") if __name__ == "__main__": tests = PrintMonkeyMountainTests() tests.test_default_case()