Python - unittest

  • Let's understand implementation of unittest by writing the below shown code in test_module1.py
from sample_module import add2num

import unittest

class Testadd2num(unittest.TestCase):

    def test_sum_2pos_num(self):
      self.assertEqual(add2num(6, 7), 13)

    def test_sum_1pos_and_1neg_num(self):
      self.assertEqual(add2num(-10, 9), -1)
python -m unittest test_module1

Output

..
---------------------------------
Ran 2 tests in 0.001s

OK
  • The output shows passing of two tests successfully. Verbose output can be viewed by adding -v at the end as shown below.
python -m unittest test_module1 -v

python -m unittest test_module1.Testadd2num

Output

..
---------------------------------
Ran 2 tests in 0.001s

OK
python -m unittest test_module1.Testadd2num.test_sum_2pos_num

Output

.
---------------------------------
Ran 1 tests in 0.001s

OK
if __name__ == '__main__':
    unittest.main()

python test_module1.py

Output

..
---------------------------------
Ran 2 tests in 0.001s

OK
def pow2num(x, y):
    """Returns x power of y.
    >>> pow2num(2, 6)
    64
    >>> pow2num(10, -3)
    0.001
    """
    return x**y
  • Now add the below Test class to test_module1.py
class Testpow2num(unittest.TestCase):

    def test_pow_2pos_num(self):
        self.assertEqual(pow2num(3, 4), 81)

    def test_neg_pow(self):
        self.assertEqual(pow2num(10, -2), 0.01)

    def test_negnum_pow(self):
        self.assertEqual(pow2num(-3, 3), -26)
python test_module1.py

Output

...F.
======================================================================
FAIL: test_negnum_pow (test_module1.Testpow2num)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\python 3.5\test_module1.py", line 26, in test_negnum_pow
    self.assertEqual(pow2num(-3, 3), -26)
AssertionError: -27 != -26

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (failures=1)
.....
class Testadd2num(unittest.TestCase):

    def setUp(self):
        print('Executed before start of every test')

    def tearDown(self):
        print('Executed at the end of every test')
        
    def test_sum_2pos_num(self):
        .....

    .....
Executed before start of every test
Executed at the end of every test
. Executed before start of every test
Executed at the end of every test
....
-------------------------------
Ran 5 tests in 0.010s

OK
.....

class Testadd2num(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print('Executed before any test in the class runs.')

    @classmethod
    def tearDownClass(cls):
        print('Executed after all tests in class are run.'

    ....

Output

Executed before any test in the class runs
Executed before start of every test
Executed at the end of every test
.Executed before start of every test
Executed at the end of every test
.Executed after all tests in class are run
...
------------------------
Ran 5 tests in 0.010s

OK
.... 

def setUpModule():
    print('Executed before an test in the module')

def tearDownModule():
    print('Executed after all tests in module are run')
    
class Testadd2num(unittest.TestCase):
   ....

Output

Executed before an test in the module is run
Executed before any test in the class runs
Executed before start of every test
Executed at the end of every test
.Executed before start of every test
Executed at the end of every test
.Executed after all tests in class are run
...Executed after all tests in module are run
-----------
Ran 5 tests in 0.010s

OK
from proj.sample_module import add2num

import unittest


class SampleTestClass(unittest.TestCase):


    def test_sample1(self):
        self.assertRaises(TypeError, add2num, 3, 'hello')
  • Running the above test with below command, passes the test.
python -m unittest test.test_module2
from proj.sample_module import add2num
import unittest

class SampleTestClass(unittest.TestCase):

    def test_sample1(self):
        with self.assertRaises(TypeError) as e:
            r = add2num(3, 'hello')
        self.assertEqual(str(e.exception), "unsupported operand type(s) for +: 'int' and 'str'")
@unittest.skip('reason for skipping')
def test_sample(self):
   ....
@unittest.skipIf(condition, reason for skipping)
def test_sample(self):
   ....
@unittest.skipUnless(condition, reason for skipping)
def test_sample(self):
   ....






No comments:

Post a Comment