Unit testing with python
Create a class that tests the functionality of a Calculator class.
Note: The
setUpClass
method below must have the@classmethod
decorator above it.
import unittest
import calculator
def setUpModule():
print('set up module')
def tearDownModule():
print('tear down module')
class TestCalculator(unittest.TestCase):
@classmethod
def setUpClass(self):
print('Set up class')
# Create an instance of the calculator that can be used in all tests
@classmethod
def tearDownClass(self):
print('Tear down class')
def test_add(self):
self.assertEqual(self.calc.add(2, 7), 9)
# Write test methods for subtract, multiply, and divide
if __name__ == '__main__':
unittest.main()
class Calculator():
"""Performs the four basic mathematical operations
Methods:
add(number, number)
subtract(number, number)
multiply(number, number)
divide(number,number)
"""
def add(self, firstOperand, secondOperand):
"""Adds two numbers together
Arguments:
firstOperand - Any number
secondOperand - An number
"""
return firstOperand + secondOperand
python CalcTest.py -v