calculator.py
#A simple calculator
class Calculator:
#empty constructor
def __init__(self):
pass
#add method - given two numbers, return the addition
def add(self, x1, x2):
return x1 + x2
#multiply method - given two numbers, return the
#multiplication of the two
def multiply(self, x1, x2):
return x1 * x2
#subtract method - given two numbers, return the value
#of first value minus the second
def subtract(self, x1, x2):
return x1 - x2
#divide method - given two numbers, return the value
#of first value divided by the second
def divide(self, x1, x2):
if x2 != 0:
return x1/x2
test_calculator.py
import unittest
from calculator import Calculator
#Test cases to test Calulator methods
#You always create a child class derived from unittest.TestCase
class TestCalculator(unittest.TestCase):
#setUp method is overridden from the parent class TestCase
def setUp(self):
self.calculator = Calculator()
#Each test method starts with the keyword test_
def test_add(self):
self.assertEqual(self.calculator.add(4,7), 11)
def test_add2(self):
self.assertEqual(self.calculator.add(7,4), 11)
def test_subtract(self):
self.assertEqual(self.calculator.subtract(10,5), 5)
def test_multiply(self):
self.assertEqual(self.calculator.multiply(3,7), 21)
def test_divide(self):
self.assertEqual(self.calculator.divide(10,2), 5)
# Executing the tests in the above test case class
if __name__ == "__main__":
unittest.main()
Output:
</pre> Ran 5 tests in 0.001s OK <pre>
Last Updated on 2022-11-30 by gantovnik
Recent Comments