Skip to content

Instantly share code, notes, and snippets.

@ivanleoncz
Last active March 26, 2019 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanleoncz/50f2f70268e209280ee90fe184033ecb to your computer and use it in GitHub Desktop.
Save ivanleoncz/50f2f70268e209280ee90fe184033ecb to your computer and use it in GitHub Desktop.
Example of Unit Testing.
import time
class Calc:
""" Arithmetic Operations for two numbers, per function. """
def add(self, n1, n2):
""" Performs addition of two numbers. """
time.sleep(5) # intentional sleep for measuring time consumed
return n1 + n2
def sub(self, n1, n2):
""" Performs subtraction of two numbers. """
return n1 - n2
def mult(self, n1, n2):
""" Performs multiplication of two numbers. """
return n1 * n2
def div(self, n1, n2):
""" Performs division of two numbers. """
return n1 // n2
import calculation
import unittest
calc = calculation.Calc()
class Probe(unittest.TestCase):
""" Probe for testing Calc class and its methods. """
def test_add(self):
""" Testing add() method from Calc class. """
self.assertEqual(calc.add(3, 3), 6)
def test_sub(self):
""" Testing sub() method from Calc class. """
self.assertEqual(calc.sub(3, 3), 0)
def test_mult(self):
""" Testing mult() method from Calc class. """
self.assertEqual(calc.mult(3, 3), 9)
def test_div(self):
""" Testing div() method from Calc class. """
self.assertEqual(calc.div(3, 3), 1)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment