import unittest import io import re from unittest import mock import math import copy import inspect import student_X as main #Replace student_X with your filename class MidpointTests(unittest.TestCase): def test_default_case(self): #make sure midpoint isn't in the Point class try: main.Point.midpoint test = False except: test = True message = "midpoint() should not be a method in the Point class. Just make it a regular function." self.assertTrue(test, message) #on with the rest of the tests! function = main.midpoint test = False try: incorrect = "" result = function.__doc__ result = result.strip() test = result != incorrect except: pass message = "No docstring." self.assertTrue(test, message) a = main.Point(0, 0) b = main.Point(0, 0) inputs = (a, b) tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = main.Point(0, 0) correct_printed = "" #run the test 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() test = isinstance(result, main.Point) message = "This function needs to return a Point." self.assertTrue(test, message) test = correct_printed == printed_output message = "Shouldn't print anything out." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work when both parameters are at the origin." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Shouldn't modify the inputs." self.assertTrue(test, message) a = main.Point(-40, 31) b = main.Point(-40, 31) inputs = (a, b) tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = main.Point(-40, 31) correct_printed = "" #run the test 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() test = isinstance(result, main.Point) message = "This function needs to return a Point." self.assertTrue(test, message) test = correct_printed == printed_output message = "Shouldn't print anything out." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work when the parameters are equal." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Shouldn't modify the inputs." self.assertTrue(test, message) a = main.Point(10, 10) b = main.Point(-10, 10) inputs = (a, b) tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = main.Point(0, 10) correct_printed = "" #run the test 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() test = isinstance(result, main.Point) message = "This function needs to return a Point." self.assertTrue(test, message) test = correct_printed == printed_output message = "Shouldn't print anything out." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work on (-10, 10) and (10, 10)." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Shouldn't modify the inputs." self.assertTrue(test, message) if __name__ == "__main__": tests = MidpointTests() tests.test_default_case()