** Unit Testing Your Code with the unittest Module
==========================================
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
if y == 0:
raise ValueError('Can not divide by zero!')
return x / y
print(add(10,5))
---------------------------------
15
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
result = calc.add(10, 5)
self.assertEqual(result, 15)
-----------calc.py-----------------
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
if y == 0:
raise ValueError('Can not divide by zero!')
return x / y
---------------------------------
C:\Users\purunet\Documents\py6>python -m unittest test_calc.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
result = calc.add(10, 5)
self.assertEqual(result, 15)
if __name__ == '__main__':
unittest.main()
-----------calc.py-----------------
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
if y == 0:
raise ValueError('Can not divide by zero!')
return x / y
---------------------------------
C:\Users\purunet\Documents\py6>python -m unittest test_calc.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def add_test(self):
result = calc.add(10, 5)
self.assertEqual(result, 15)
if __name__ == '__main__':
unittest.main()
---------------------------------
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
result = calc.add(10, 5)
self.assertEqual(result, 15)
if __name__ == '__main__':
unittest.main()
---------------------------------
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
result = calc.add(10, 5)
self.assertEqual(result, 14)
if __name__ == '__main__':
unittest.main()
---------------------------------
F
======================================================================
FAIL: test_add (__main__.TestCalc)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\purunet\Documents\py6\test_calc.py", line 9, in test_add
self.assertEqual(result, 14)
AssertionError: 15 != 14
----------------------------------------------------------------------
Ran 1 test in 0.013s
FAILED (failures=1)
[Finished in 0.5s with exit code 1]
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
self.assertEqual(calc.add(10, 5), 15)
self.assertEqual(calc.add(-1, 1), 0)
self.assertEqual(calc.add(-1, -1), -2)
if __name__ == '__main__':
unittest.main()
---------------------------------
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
self.assertEqual(calc.add(10, 5), 15)
self.assertEqual(calc.add(-1, 1), 0)
self.assertEqual(calc.add(-1, -1), -2)
def test_subtract(self):
self.assertEqual(calc.subtract(10, 5), 5)
self.assertEqual(calc.subtract(-1, 1), -2)
self.assertEqual(calc.subtract(-1, -1), 0)
def test_multiply(self):
self.assertEqual(calc.multiply(10, 5), 50)
self.assertEqual(calc.multiply(-1, 1), -1)
self.assertEqual(calc.multiply(-1, -1), 1)
def test_divide(self):
self.assertEqual(calc.divide(10, 5), 2)
self.assertEqual(calc.divide(-1, 1), -1)
self.assertEqual(calc.divide(-1, -1), 1)
self.assertEqual(calc.divide(5, 2), 2.5)
if __name__ == '__main__':
unittest.main()
---------------------------------
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
self.assertEqual(calc.add(10, 5), 15)
self.assertEqual(calc.add(-1, 1), 0)
self.assertEqual(calc.add(-1, -1), -2)
def test_subtract(self):
self.assertEqual(calc.subtract(10, 5), 5)
self.assertEqual(calc.subtract(-1, 1), -2)
self.assertEqual(calc.subtract(-1, -1), 0)
def test_multiply(self):
self.assertEqual(calc.multiply(10, 5), 50)
self.assertEqual(calc.multiply(-1, 1), -1)
self.assertEqual(calc.multiply(-1, -1), 1)
def test_divide(self):
self.assertEqual(calc.divide(10, 5), 2)
self.assertEqual(calc.divide(-1, 1), -1)
self.assertEqual(calc.divide(-1, -1), 1)
self.assertEqual(calc.divide(5, 2), 2.5)
if __name__ == '__main__':
unittest.main()
-----------calc.py-----------------
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
# error
return x ** y
def divide(x, y):
"""Divide Function"""
if y == 0:
raise ValueError('Can not divide by zero!')
return x / y
---------------------------------
..F.
======================================================================
FAIL: test_multiply (__main__.TestCalc)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\purunet\Documents\py6\test_calc.py", line 18, in test_multiply
self.assertEqual(calc.multiply(10, 5), 50)
AssertionError: 100000 != 50
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=1)
[Finished in 0.5s with exit code 1]
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
self.assertEqual(calc.add(10, 5), 15)
self.assertEqual(calc.add(-1, 1), 0)
self.assertEqual(calc.add(-1, -1), -2)
def test_subtract(self):
self.assertEqual(calc.subtract(10, 5), 5)
self.assertEqual(calc.subtract(-1, 1), -2)
self.assertEqual(calc.subtract(-1, -1), 0)
def test_multiply(self):
self.assertEqual(calc.multiply(10, 5), 50)
self.assertEqual(calc.multiply(-1, 1), -1)
self.assertEqual(calc.multiply(-1, -1), 1)
def test_divide(self):
self.assertEqual(calc.divide(10, 5), 2)
self.assertEqual(calc.divide(-1, 1), -1)
self.assertEqual(calc.divide(-1, -1), 1)
self.assertEqual(calc.divide(5, 2), 2.5)
self.assertRaises(ValueError, calc.divide, 10, 0)
if __name__ == '__main__':
unittest.main()
-----------calc.py-----------------
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
# error
return x ** y
def divide(x, y):
"""Divide Function"""
if y == 0:
raise ValueError('Can not divide by zero!')
return x / y
---------------------------------
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
self.assertEqual(calc.add(10, 5), 15)
self.assertEqual(calc.add(-1, 1), 0)
self.assertEqual(calc.add(-1, -1), -2)
def test_subtract(self):
self.assertEqual(calc.subtract(10, 5), 5)
self.assertEqual(calc.subtract(-1, 1), -2)
self.assertEqual(calc.subtract(-1, -1), 0)
def test_multiply(self):
self.assertEqual(calc.multiply(10, 5), 50)
self.assertEqual(calc.multiply(-1, 1), -1)
self.assertEqual(calc.multiply(-1, -1), 1)
def test_divide(self):
self.assertEqual(calc.divide(10, 5), 2)
self.assertEqual(calc.divide(-1, 1), -1)
self.assertEqual(calc.divide(-1, -1), 1)
self.assertEqual(calc.divide(5, 2), 2.5)
self.assertRaises(ValueError, calc.divide, 10, 2)
if __name__ == '__main__':
unittest.main()
-----------calc.py-----------------
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
# error
return x ** y
def divide(x, y):
"""Divide Function"""
if y == 0:
raise ValueError('Can not divide by zero!')
return x / y
---------------------------------
.F..
======================================================================
FAIL: test_divide (__main__.TestCalc)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\purunet\Documents\py6\test_calc.py", line 28, in test_divide
self.assertRaises(ValueError, calc.divide, 10, 2)
AssertionError: ValueError not raised by divide
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=1)
[Finished in 0.4s with exit code 1]
---------------------------------
==========================================
import unittest
import calc
class TestCalc(unittest.TestCase):
def test_add(self):
self.assertEqual(calc.add(10, 5), 15)
self.assertEqual(calc.add(-1, 1), 0)
self.assertEqual(calc.add(-1, -1), -2)
def test_subtract(self):
self.assertEqual(calc.subtract(10, 5), 5)
self.assertEqual(calc.subtract(-1, 1), -2)
self.assertEqual(calc.subtract(-1, -1), 0)
def test_multiply(self):
self.assertEqual(calc.multiply(10, 5), 50)
self.assertEqual(calc.multiply(-1, 1), -1)
self.assertEqual(calc.multiply(-1, -1), 1)
def test_divide(self):
self.assertEqual(calc.divide(10, 5), 2)
self.assertEqual(calc.divide(-1, 1), -1)
self.assertEqual(calc.divide(-1, -1), 1)
self.assertEqual(calc.divide(5, 2), 2.5)
with self.assertRaises(ValueError):
calc.divide(10, 0)
if __name__ == '__main__':
unittest.main()
-----------calc.py-----------------
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
# error
return x ** y
def divide(x, y):
"""Divide Function"""
if y == 0:
raise ValueError('Can not divide by zero!')
return x / y
---------------------------------
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
---------------------------------
==========================================
import unittest
from employee import Employee
class TestEmployee(unittest.TestCase):
def test_email(self):
emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Sue', 'Smith', 60000)
self.assertEqual(emp_1.email, 'Corey.Schafer@email.com')
self.assertEqual(emp_2.email, 'Sue.Smith@email.com')
emp_1.first = 'John'
emp_2.first = 'Jane'
self.assertEqual(emp_1.email, 'John.Schafer@email.com')
self.assertEqual(emp_2.email, 'Jane.Smith@email.com')
def test_fullname(self):
emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Sue', 'Smith', 60000)
self.assertEqual(emp_1.fullname, 'Corey Schafer')
self.assertEqual(emp_2.fullname, 'Sue Smith')
emp_1.first = 'John'
emp_2.first = 'Jane'
self.assertEqual(emp_1.fullname, 'John Schafer')
self.assertEqual(emp_2.fullname, 'Jane Smith')
def test_apply_raise(self):
emp_1 = Employee('Corey', 'Schafer', 50000)
emp_2 = Employee('Sue', 'Smith', 60000)
emp_1.apply_raise()
emp_2.apply_raise()
self.assertEqual(emp_1.pay, 52500)
self.assertEqual(emp_2.pay, 63000)
if __name__ == '__main__':
unittest.main()
-----------employee.py--------------
import requests
class Employee:
"""A sample Employee class"""
raise_amt = 1.05
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def monthly_schedule(self, month):
response = requests.get(f'http://company.com/{self.last}/{month}')
if response.ok:
return response.text
else:
return 'Bad Response!'
---------------------------------
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
---------------------------------
==========================================
import unittest
from employee import Employee
class TestEmployee(unittest.TestCase):
def setUp(self):
self.emp_1 = Employee('Corey', 'Schafer', 50000)
self.emp_2 = Employee('Sue', 'Smith', 60000)
def tearDown(self):
pass
def test_email(self):
self.assertEqual(self.emp_1.email, 'Corey.Schafer@email.com')
self.assertEqual(self.emp_2.email, 'Sue.Smith@email.com')
self.emp_1.first = 'John'
self.emp_2.first = 'Jane'
self.assertEqual(self.emp_1.email, 'John.Schafer@email.com')
self.assertEqual(self.emp_2.email, 'Jane.Smith@email.com')
def test_fullname(self):
self.assertEqual(self.emp_1.fullname, 'Corey Schafer')
self.assertEqual(self.emp_2.fullname, 'Sue Smith')
self.emp_1.first = 'John'
self.emp_2.first = 'Jane'
self.assertEqual(self.emp_1.fullname, 'John Schafer')
self.assertEqual(self.emp_2.fullname, 'Jane Smith')
def test_apply_raise(self):
self.emp_1.apply_raise()
self.emp_2.apply_raise()
self.assertEqual(self.emp_1.pay, 52500)
self.assertEqual(self.emp_2.pay, 63000)
if __name__ == '__main__':
unittest.main()
-----------employee.py--------------
import requests
class Employee:
"""A sample Employee class"""
raise_amt = 1.05
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def monthly_schedule(self, month):
response = requests.get(f'http://company.com/{self.last}/{month}')
if response.ok:
return response.text
else:
return 'Bad Response!'
---------------------------------
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
---------------------------------
==========================================
import unittest
from employee import Employee
class TestEmployee(unittest.TestCase):
def setUp(self):
print('setUp')
self.emp_1 = Employee('Corey', 'Schafer', 50000)
self.emp_2 = Employee('Sue', 'Smith', 60000)
def tearDown(self):
print('tearDown\n')
def test_email(self):
print('test_email')
self.assertEqual(self.emp_1.email, 'Corey.Schafer@email.com')
self.assertEqual(self.emp_2.email, 'Sue.Smith@email.com')
self.emp_1.first = 'John'
self.emp_2.first = 'Jane'
self.assertEqual(self.emp_1.email, 'John.Schafer@email.com')
self.assertEqual(self.emp_2.email, 'Jane.Smith@email.com')
def test_fullname(self):
print('test_fullname')
self.assertEqual(self.emp_1.fullname, 'Corey Schafer')
self.assertEqual(self.emp_2.fullname, 'Sue Smith')
self.emp_1.first = 'John'
self.emp_2.first = 'Jane'
self.assertEqual(self.emp_1.fullname, 'John Schafer')
self.assertEqual(self.emp_2.fullname, 'Jane Smith')
def test_apply_raise(self):
print('test_apply_raise')
self.emp_1.apply_raise()
self.emp_2.apply_raise()
self.assertEqual(self.emp_1.pay, 52500)
self.assertEqual(self.emp_2.pay, 63000)
if __name__ == '__main__':
unittest.main()
-----------employee.py--------------
import requests
class Employee:
"""A sample Employee class"""
raise_amt = 1.05
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def monthly_schedule(self, month):
response = requests.get(f'http://company.com/{self.last}/{month}')
if response.ok:
return response.text
else:
return 'Bad Response!'
---------------------------------
setUp
test_apply_raise
tearDown
.setUp
test_email
tearDown
.setUp.
test_fullname
tearDown
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
---------------------------------
==========================================
import unittest
from employee import Employee
class TestEmployee(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('setupClass')
@classmethod
def tearDownClass(cls):
print('teardownClass')
def setUp(self):
print('setUp')
self.emp_1 = Employee('Corey', 'Schafer', 50000)
self.emp_2 = Employee('Sue', 'Smith', 60000)
def tearDown(self):
print('tearDown\n')
def test_email(self):
print('test_email')
self.assertEqual(self.emp_1.email, 'Corey.Schafer@email.com')
self.assertEqual(self.emp_2.email, 'Sue.Smith@email.com')
self.emp_1.first = 'John'
self.emp_2.first = 'Jane'
self.assertEqual(self.emp_1.email, 'John.Schafer@email.com')
self.assertEqual(self.emp_2.email, 'Jane.Smith@email.com')
def test_fullname(self):
print('test_fullname')
self.assertEqual(self.emp_1.fullname, 'Corey Schafer')
self.assertEqual(self.emp_2.fullname, 'Sue Smith')
self.emp_1.first = 'John'
self.emp_2.first = 'Jane'
self.assertEqual(self.emp_1.fullname, 'John Schafer')
self.assertEqual(self.emp_2.fullname, 'Jane Smith')
def test_apply_raise(self):
print('test_apply_raise')
self.emp_1.apply_raise()
self.emp_2.apply_raise()
self.assertEqual(self.emp_1.pay, 52500)
self.assertEqual(self.emp_2.pay, 63000)
if __name__ == '__main__':
unittest.main()
-----------employee.py--------------
import requests
class Employee:
"""A sample Employee class"""
raise_amt = 1.05
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def monthly_schedule(self, month):
response = requests.get(f'http://company.com/{self.last}/{month}')
if response.ok:
return response.text
else:
return 'Bad Response!'
---------------------------------
setupClass
setUp
test_apply_raise
tearDown
.setUp
test_email
tearDown
.setUp
test_fullname
tearDown
.teardownClass
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
---------------------------------
==========================================
import unittest
from unittest.mock import patch
from employee import Employee
class TestEmployee(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('setupClass')
@classmethod
def tearDownClass(cls):
print('teardownClass')
def setUp(self):
print('setUp')
self.emp_1 = Employee('Corey', 'Schafer', 50000)
self.emp_2 = Employee('Sue', 'Smith', 60000)
def tearDown(self):
print('tearDown\n')
def test_email(self):
print('test_email')
self.assertEqual(self.emp_1.email, 'Corey.Schafer@email.com')
self.assertEqual(self.emp_2.email, 'Sue.Smith@email.com')
self.emp_1.first = 'John'
self.emp_2.first = 'Jane'
self.assertEqual(self.emp_1.email, 'John.Schafer@email.com')
self.assertEqual(self.emp_2.email, 'Jane.Smith@email.com')
def test_fullname(self):
print('test_fullname')
self.assertEqual(self.emp_1.fullname, 'Corey Schafer')
self.assertEqual(self.emp_2.fullname, 'Sue Smith')
self.emp_1.first = 'John'
self.emp_2.first = 'Jane'
self.assertEqual(self.emp_1.fullname, 'John Schafer')
self.assertEqual(self.emp_2.fullname, 'Jane Smith')
def test_apply_raise(self):
print('test_apply_raise')
self.emp_1.apply_raise()
self.emp_2.apply_raise()
self.assertEqual(self.emp_1.pay, 52500)
self.assertEqual(self.emp_2.pay, 63000)
def test_monthly_schedule(self):
with patch('employee.requests.get') as mocked_get:
mocked_get.return_value.ok = True
mocked_get.return_value.text = 'Success'
schedule = self.emp_1.monthly_schedule('May')
mocked_get.assert_called_with('http://company.com/Schafer/May')
self.assertEqual(schedule, 'Success')
mocked_get.return_value.ok = False
schedule = self.emp_2.monthly_schedule('June')
mocked_get.assert_called_with('http://company.com/Smith/June')
self.assertEqual(schedule, 'Bad Response!')
if __name__ == '__main__':
unittest.main()
-----------employee.py--------------
import requests
class Employee:
"""A sample Employee class"""
raise_amt = 1.05
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
def apply_raise(self):
self.pay = int(self.pay * self.raise_amt)
def monthly_schedule(self, month):
response = requests.get(f'http://company.com/{self.last}/{month}')
if response.ok:
return response.text
else:
return 'Bad Response!'
---------------------------------
setupClass
setUp
test_apply_raise
tearDown
.
setUp
test_email
tearDown
.setUp
test_fullname
tearDown
.setUp
tearDown
.
teardownClass
----------------------------------------------------------------------
Ran 4 tests in 0.022s
OK
---------------------------------