Advertisement
cirossmonteiro

Number, operator overloading, unittest

Jun 29th, 2022
671
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import unittest
  2.  
  3. class Number:
  4.     def __init__(self, value):
  5.         self.value = value
  6.  
  7.    
  8.     def __add__(self, other_Number):
  9.         """
  10.        teaching how to sum two instances of Number
  11.        """
  12.         return Number(self.value+other_Number.value)
  13.  
  14. class NumberTest(unittest.TestCase):
  15.  
  16.     def setUp(self):
  17.         self.one = Number(1)
  18.         self.two = Number(2)
  19.  
  20.     def test_add(self):
  21.         # first assertion
  22.         self.assertEqual(
  23.             (self.one + self.two).value,
  24.             self.one.value + self.two.value
  25.         )
  26.  
  27.         # second assertion
  28.         self.assertEqual(
  29.             (self.one + self.two).value,
  30.             Number(3).value
  31.         )
  32.  
  33. # it'll run the test if the script is started by terminal, not imported
  34. if __name__ == '__main__':
  35.     unittest.main()
  36.  
Advertisement
RAW Paste Data Copied
Advertisement