Advertisement
ugochukwu15

Untitled

Aug 26th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import unittest
  2.  
  3. class Test(unittest.TestCase):
  4.     def test_returns_correct_power(self):
  5.         base, exp = 2, 3
  6.         res = power(base, exp)
  7.         self.assertEqual(
  8.             res,
  9.             8,
  10.             msg='Expected {}, got {}'.format(8, res)
  11.         )
  12.  
  13.     def test_return_1_when_exp_is_0(self):
  14.         base, exp = 4, 0
  15.         res = power(base, exp)
  16.         self.assertEqual(
  17.             res,
  18.             1,
  19.             msg='A number power 0 should be 1'
  20.         )
  21.  
  22.     def test_return_0_when_base_is_0(self):
  23.         base, exp = 0, 10
  24.         res = power(base, exp)
  25.         self.assertEqual(
  26.             res,
  27.             0,
  28.             msg='O power any number should be 0'
  29.         )
  30.  
  31.     def test_non_digit_argument(self):
  32.         with self.assertRaises(TypeError) as context:
  33.             base, exp = 'base', 'exp'
  34.             res = power(base, exp)
  35.             self.assertEqual(
  36.                 'Argument must be interfer or float',
  37.                 context.exception.message,
  38.                 'Only digits are allowed as input'
  39.             )
  40.  
  41.     def test_if_recursive(self):
  42.         # Python 3 will raise RecursionError, Python 2 will raise RuntimeError.
  43.         # Instead of asserting it raises either of those, assert it raises an
  44.         # error and check the message, which is the same across the Py versions.
  45.         with self.assertRaises(Exception) as context:
  46.             power(100000, 5)
  47.             self.assertIn(
  48.                 'maximum recursion depth exceeded',
  49.                 context.exception.message,
  50.                 msg='You must use recursion when implementing replicate_recur.'
  51.             )
  52.  
  53. def pow(x, y):
  54.   result = 1
  55.   for i in range(y):   # using _ to indicate throwaway iteration variable
  56.     result *= x
  57.   return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement