Python Tutorial

Thursday, April 25, 2013

Python unittest

Unittest is included in the Python standard library. It is very easy to use. Here I write two function named myfun, reverse and then unsing MyTest function I tested those two functions. Let's check.

import unittest

def myfun(a, b):
    return a+b

def reverse(A):
    i = 0
    j = len(A)-1
    while i<j:
        A[i], A[j] = A[j], A[i]
        i = i + 1
        j = j -1

class MyTest(unittest.TestCase):
    def test(self):
        self.assertEqual(myfun(2, 3), 5)
        self.assertEqual(myfun(4, 3), 7)

        # test reverse(A)
        A = [1, 2, 3]
        expected = [3, 2, 1]
        reverse(A)
        self.assertEqual(A, expected)
        

if __name__ == '__main__':
    unittest.main(exit=False)

0 comments:

Post a Comment