Calculator Unit Tests
Just like you did in JavaScript when you learned about Jasmine, you're going to create a class that test the functionality of a Calculator class.
Starter code for test 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)
if name == 'main': unittest.main() Starter code for calculator class
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
Running the test class
python CalcTest.py -v