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 LocationBasicsTests(unittest.TestCase): def test_default_case(self): function = main.Location.__str__ #check the docstrings incorrect = "" try: doc = main.Location.__doc__ doc = doc.strip() except: doc = "" test = doc != incorrect message = "No docstring for the Location class." self.assertTrue(test, message) try: doc = main.Location.__init__.__doc__ doc = doc.strip() except: doc = "" test = doc != incorrect message = "No docstring for Location.__init__." self.assertTrue(test, message) try: doc = main.Location.__str__.__doc__ doc = doc.strip() except: doc = "" test = doc != incorrect message = "No docstring for Location.__str__." self.assertTrue(test, message) buoy_latitude = main.GeographicCoordinate('N', 0, 0, 0) buoy_longitude = main.GeographicCoordinate('E', 0, 0, 0) buoy_location = main.Location(buoy_latitude, buoy_longitude) #main.Pokemon.__eq__ = lambda x,y: x.name == y.name and x.number == y.number and x.types == y.types and x.hit_points == y.hit_points and x.max_hp == y.max_hp inputs = (buoy_location) tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = "(0\N{DEGREE SIGN}0'0\"N, 0\N{DEGREE SIGN}0'0\"E)" 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 = correct_printed == printed_output message = "Shouldn't print anything." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work on the location of Station 13010 (in the Gulf of Guinea)." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Inputs modified." self.assertTrue(test, message) mw_latitude = main.GeographicCoordinate('N', 44, 16, 13.8) mw_longitude = main.GeographicCoordinate('W', 71, 18, 11.7) mw_location = main.Location(mw_latitude, mw_longitude) #main.Pokemon.__eq__ = lambda x,y: x.name == y.name and x.number == y.number and x.types == y.types and x.hit_points == y.hit_points and x.max_hp == y.max_hp inputs = (mw_location) tested_inputs = inputs #change this if you need to test specific inputs #expected answers correct_inputs = inputs correct_result = "(44\N{DEGREE SIGN}16'13.8\"N, 71\N{DEGREE SIGN}18'11.7\"W)" 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 = correct_printed == printed_output message = "Shouldn't print anything." self.assertTrue(test, message) test = result == correct_result message = "Doesn't work on the location of Mount Washington." self.assertTrue(test, message) test = tested_inputs == correct_inputs message = "Inputs modified." self.assertTrue(test, message) if __name__ == "__main__": tests = LocationBasicsTests() tests.test_default_case()