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 GetNumDoorsAndSetHasHatchTests(unittest.TestCase): def test_default_case(self): #check that Sedan and Coupe exist classname = "Sedan" try: main.Sedan #check that Sedan class exists classname = "Coupe" main.Coupe #check that Coupe class exists except: message = "Class " + classname + " doesn't exist." test = False self.assertTrue(test, message) try: main.Car.get_num_doors test = False except: test = True message = "get_num_doors shouldn't be in the Car class, only in the subclasses." self.assertTrue(test, message) try: main.Car.set_has_hatch except: message = "Method set_has_hatch should be in the Car class, not in the subclasses." test = False self.assertTrue(test, message) #test docstrings incorrect = "" try: doc = main.Car.set_has_hatch.__doc__ doc = doc.strip() except: doc = "" test = doc != incorrect message = "No docstring for set_has_hatch." self.assertTrue(test, message) try: doc = main.Coupe.__doc__ doc = doc.strip() except: doc = "" test = doc != incorrect message = "No docstring for Coupe." self.assertTrue(test, message) try: doc = main.Sedan.__doc__ doc = doc.strip() except: doc = "" test = doc != incorrect message = "No docstring for Sedan." self.assertTrue(test, message) try: doc = main.Coupe.get_num_doors.__doc__ doc = doc.strip() except: doc = "" test = doc != incorrect message = "No docstring for Coupe.get_num_doors." self.assertTrue(test, message) try: doc = main.Sedan.get_num_doors.__doc__ doc = doc.strip() except: doc = "" test = doc != incorrect message = "No docstring for Sedan.get_num_doors." self.assertTrue(test, message) jackelope = main.Sedan(1991, 'beige', 'Corolla') function = jackelope.get_num_doors inputs = () tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = 4 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() #this is a cheat. Take this out for later ones! #correct_result = result test = correct_printed == printed_output message = "Shouldn't print anything." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work on a sedan with no hatch." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Modifies inputs." self.assertTrue(test, message) jackelope = main.Coupe(1991, 'beige', 'Corolla') function = jackelope.get_num_doors inputs = () tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = 2 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() #this is a cheat. Take this out for later ones! #correct_result = result test = correct_printed == printed_output message = "Shouldn't print anything." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work on a coupe with no hatch." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Modifies inputs." self.assertTrue(test, message) jackelope = main.Coupe(1991, 'beige', 'Corolla') function = jackelope.get_num_doors x = jackelope try: x.set_has_hatch(False) test = True except: test = False message = "set_has_hatch isn't really working." self.assertTrue(test, message) inputs = () tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = 2 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() #this is a cheat. Take this out for later ones! #correct_result = result test = correct_printed == printed_output message = "Shouldn't print anything." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work on a coupe with no hatch." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Modifies inputs." self.assertTrue(test, message) jackelope = main.Coupe(1991, 'beige', 'Corolla') function = jackelope.get_num_doors x = jackelope try: x.set_has_hatch(True) test = True except: test = False message = "set_has_hatch isn't really working." self.assertTrue(test, message) inputs = () tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = 3 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() #this is a cheat. Take this out for later ones! #correct_result = result test = correct_printed == printed_output message = "Shouldn't print anything." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work on a coupe with a hatchback door." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Modifies inputs." self.assertTrue(test, message) jackelope = main.Sedan(1991, 'beige', 'Corolla') function = jackelope.get_num_doors x = jackelope try: x.set_has_hatch(True) test = True except: test = False message = "set_has_hatch isn't really working." self.assertTrue(test, message) inputs = () tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = 5 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() #this is a cheat. Take this out for later ones! #correct_result = result test = correct_printed == printed_output message = "Shouldn't print anything." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work on a sedan with a hatchback door." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Modifies inputs." self.assertTrue(test, message) if __name__ == "__main__": tests = GetNumDoorsAndSetHasHatchTests() tests.test_default_case()