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 PrintWhetherNLooksPrimeTests(unittest.TestCase): def test_default_case(self): function = main.print_whether_n_looks_prime 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(15, 1) output = fake_stdout.getvalue().strip() correct = "15" in output feedback = "Doesn't print out the first parameter." self.assertTrue(correct, feedback) regex = "15 looks like it('| i)s (a |)prime.?" correct = re.match(regex, output) feedback = "Doesn't print out the correct string when the second parameter is 1." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(15, 2) output = fake_stdout.getvalue().strip() correct = "15" in output feedback = "Doesn't print out the first parameter." self.assertTrue(correct, feedback) regex = "15 looks like it('| i)s (a |)prime.?" correct = re.match(regex, output) feedback = "Doesn't print out the correct string when the second parameter is lower than any prime factors of the first parameter." self.assertTrue(correct, feedback) self.assertRegex(fake_stdout.getvalue().strip(), "15 looks like it('| i)s (a |)prime.?") with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(15, 3) output = fake_stdout.getvalue().strip() correct = "15" in output feedback = "Doesn't print out the first parameter." self.assertTrue(correct, feedback) correct = "divisible" in output feedback = "'divisible' isn't in the output when it should be. (It might be misspelled.)" self.assertTrue(correct, feedback) regex = "15 is divisible by ?3 ?.?" correct = re.match(regex, output) feedback = "Doesn't print out the correct string when the second parameter is equal to one of the prime factors of the first parameter." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(100, 49) output = fake_stdout.getvalue().strip() correct = "100" in output feedback = "Doesn't print out the first parameter." self.assertTrue(correct, feedback) correct = "divisible" in output feedback = "'divisible' isn't in the output when it should be. (It might be misspelled.)" self.assertTrue(correct, feedback) regex = "100 is divisible by ?25 ?.?" correct = re.match(regex, output) feedback = "Doesn't print out the correct string when the second parameter is greater than one of the prime factors of the first parameter." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(53, 52) output = fake_stdout.getvalue().strip() correct = "53" in output feedback = "Doesn't print out the first parameter." self.assertTrue(correct, feedback) regex = "53 looks like it('| i)s (a |)prime.?" correct = re.match(regex, output) feedback = "Doesn't print the correct output when the first parameter is a smallish prime number." self.assertTrue(correct, feedback) if __name__ == "__main__": tests = PrintWhetherNLooksPrimeTests() tests.test_default_case()