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 import inspect class PrintAboutPositivityTests(unittest.TestCase): def test_default_case(self): function = main.print_about_positivity 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(17) output = fake_stdout.getvalue().strip() test = "17" in output feedback = "Doesn't print the parameter in the sentence." self.assertTrue(test, feedback) test = "True" in output feedback = "Doesn't print whether the number is positive in the sentence." self.assertTrue(test, feedback) regex = "It is .* that .* is positive\." test = re.match(regex, output) feedback = "Doesn't include the correct words in the correct order." self.assertTrue(test, feedback) correct = output.endswith(".") feedback = "The output doesn't end with a period." self.assertTrue(correct, feedback) correct = "It is True that 17 is positive." test = output == correct feedback = "Doesn't print out the correct sentence when the parameter is 17. Is your spacing okay?" self.assertTrue(test, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(213) output = fake_stdout.getvalue().strip() correct = "It is True that 213 is positive." test = output == correct feedback = "Doesn't print out the correct sentence with 213." self.assertTrue(test, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(-50) output = fake_stdout.getvalue().strip() correct = "It is False that -50 is positive." test = output == correct feedback = "Doesn't print out the correct sentence with a negative number." self.assertTrue(test, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(0) output = fake_stdout.getvalue().strip() correct = "It is False that 0 is positive." test = output == correct feedback = "Doesn't print out the correct sentence with zero." self.assertTrue(test, feedback) source = inspect.getsource(function) test = not ("if" in source and "else" in source) feedback = "Don't use conditionals (if) in your code." self.assertTrue(test, feedback) if __name__ == "__main__": tests = PrintAboutPositivityTests() tests.test_default_case()