Search This Blog........

Wednesday, 13 July 2016

Python unit testing

Python Unit testing tutorial



Unit test: 
Using python default unittest allows you to write more robust tests.

The unittest module  provides a runner to execute tests i.e, grouping tests in to logical unit and the concept of setup and teardown.
Lets see one example.....,
import unittest

class Test(unittest.TestCase):
    def setUp(self):
        self.num=1

    def test_math(self):
        self.assertEqual(self.num,1)


if __name__ ==  '__main__':
    unittest,main()   


Explanation
A testcase is class that inherits from unittest.
The function(def) inside class all require a mandatory parameter named self 
When running unittest.main(), that take care of finding all the testcases. Calling setUp method first (whose name start with test) and then finally calling method called tearDown()


 

No comments:

Post a Comment