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 class PrintSphereVolumeTests(unittest.TestCase): def test_default_case(self): function = main.print_sphere_volume try: doc = function.__doc__ except: doc = "" correct = doc.strip() != "" message = "docstring is blank." self.assertTrue(correct, message) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(5) output = fake_stdout.getvalue().strip() regex = ".* radius 5 has .*" feedback = "Doesn't have the correct radius (5) included in the output." self.assertRegex(output, regex, feedback) regex = "A sphere with radius 5 has volume [1234567890\.]* *\.?" feedback = "Text of the output is incorrect." self.assertRegex(output, regex, feedback) regex = "[A-Za-z ]* 5 [A-Za-z ]*523.598775598298. *\.?" feedback = "Doesn't print out the correct volume when the radius is 5." self.assertRegex(output, regex, feedback) correct = output.endswith('.') feedback = "Doesn't end with a period." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(8) output = fake_stdout.getvalue().strip() regex = ".* radius 8 has .*" feedback = "Doesn't have the correct radius (8) included in the output." self.assertRegex(output, regex, feedback) regex = "A sphere with radius 8 has volume [1234567890\.]* *\.?" feedback = "Text of the output is incorrect." self.assertRegex(output, regex, feedback) regex = "[A-Za-z ]* 8 [A-Za-z ]*2144\.66058485063. *\.?" feedback = "Doesn't print out the correct volume when the radius is 8." self.assertRegex(output, regex, feedback) correct = output.endswith('.') feedback = "Doesn't end with a period." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(3) output = fake_stdout.getvalue().strip() regex = ".* radius 3 has .*" feedback = "Doesn't have the correct radius (3) included in the output." self.assertRegex(output, regex, feedback) regex = "A sphere with radius 3 has volume [1234567890\.]* *\.?" feedback = "Text of the output is incorrect." self.assertRegex(output, regex, feedback) regex = "[A-Za-z ]* 3 [A-Za-z ]*113.0973355292325. *\.?" feedback = "Doesn't print out the correct volume when the radius is 3." self.assertRegex(output, regex, feedback) correct = output.endswith('.') feedback = "Doesn't end with a period." self.assertTrue(correct, feedback) if __name__ == "__main__": tests = PrintSphereVolumeTests() tests.test_default_case()