Creating a unit test function factorize. The factorize (x) function is given with the following contract:
def factorize(x): """ Factorize positive integer and return its factors. :type x: int,>=0 :rtype: tuple[N],N>0 """ pass Write a test suite:
test_wrong_types_raise_exception test_negative test_zero_and_one_cases test_simple_numbers test_two_simple_multipliers test_many_multipliers Check them accordingly:
That the types float and str (values ​​are 'string', 1.5) raise a TypeError exception. That for negative numbers -1, -10 and -100, a ValueError exception is thrown. That for the number 0 returns a tuple (0,), and for number 1 a tuple (1,) That for primes 3, 13, 29 returns a tuple containing one given number. That for numbers 6, 26, 121, the tuples are (2, 3), (2, 13) and (11, 11), respectively. For the numbers 1001 and 9699690, the tuples (7, 11, 13) and (2, 3, 5, 7, 11, 13, 17, 19) are returned respectively. In this case, several different checks within the same test must be processed as subcases with x: subTest (x = ...).
IMPORTANT! The variable name in the test case should be exactly "x". All input data must be as specified in the condition. In the task it is necessary to implement ONLY the TestFactorize class, other than that, nothing needs to be implemented. You also don't need to import unittest and call unittest.main () in the solution.
Posted by:
import unittest def factorize(x): """ Factorize integer positive and return its factors. :type x: int,>=0 :rtype: tuple[N],N>0 """ pass class MyTest(unittest.TestCase): def test_wrong_types_raise_exception(self): for x in ('string', 1.5): with self.subTest(x=x): self.assertRaises(TypeError, factorize, x) def test_negative(self): for x in (-1, -10, -100): with self.subTest(x=x): self.assertRaises(ValueError, factorize, x) def test_zero_and_one_cases(self): for x in (0, 1): with self.subTest(x=x): self.assertEqual(factorize(x), (x,)) def test_simple_numbers(self): for x in (3, 13, 29): with self.subTest(x=x): self.assertEqual(factorize(x), (x,)) def test_two_simple_multipliers(self): for x, answer in ((6, (2,3)), (26, (2,13)), (121, (11,11))): with self.subTest(x=x): self.assertEqual(factorize(x), answer) def test_many_multipliers(self): for x, answer in ((1001, (7, 11, 13)), (9699690, (2, 3, 5, 7, 11, 13, 17, 19))): with self.subTest(x=x): self.assertEqual(factorize(x), answer) if __name__ != "__main__": unittest.main() Gives an error! RuntimeErrorElement (RuntimeError, Error in grading script. See raw output log for details.) Help to fix the error. Thank you in advance!