Unit test module is an in-build library in python, used to write test scripts . It gives some helpful methods while helps us designing out test in a way , that emulates a real production environment.
While writing test, we need to keep these things in mind
1. Test should start with a clean slate. Meaning the subject under test must not hold any info or be in a state, that might affect the test results.
2. Every tests should test one and only one feature . Clubbing multiple tests in one test, is a bad idea.
Unit test provides some special methods. These can be used to handle the flow of tests as per scenario. Below is a generic template of a unit test showing all methods and their tasks.
If we run the above test , the results are as follows :
While writing test, we need to keep these things in mind
1. Test should start with a clean slate. Meaning the subject under test must not hold any info or be in a state, that might affect the test results.
2. Every tests should test one and only one feature . Clubbing multiple tests in one test, is a bad idea.
Unit test provides some special methods. These can be used to handle the flow of tests as per scenario. Below is a generic template of a unit test showing all methods and their tasks.
import unittest def setUpModule(): print '------------SET UP MODULE--------------' print '\nImport something here. Example configs \n' def tearDownModule(): print '------------TEAR DOWN MODULE--------------' print 'Destroy whatever you did in setUpModule \n' class MyTest(unittest.TestCase): @classmethod def setUpClass(cls): # cls is just a convention. It can be anything print '------------SET UP CLASS--------------' print 'Created something here' print '------------END----------------\n' @classmethod def tearDownClass(cls): print '------------TEAR DOWN CLASS--------------' print 'Remove whatever was created in setupclass' print '-------------END----------------\n' def setUp(self): print '------------SETUP--------------' print 'Connect to above created thing' print '------------END----------------\n' def tearDown(self): print '------------TEAR DOWN--------------' print 'Disconnect to above created thing' print '------------END----------------\n' def test_func1(self): print '------------TEST 1--------------' print 'Test function one here' print '------------END----------------\n' def test_func2(self): print '------------TEST 2--------------' print 'Test function two here' print '------------END----------------\n' if __name__ == '__main__': unittest.main()
If we run the above test , the results are as follows :
D:\myusername\Desktop\PyJobs>python -m unittest test_unittest_all_examples ------------SET UP MODULE-------------- Import something here. Example configs ------------SET UP CLASS-------------- Created something here ------------END---------------- ------------SETUP-------------- Connect to above created thing ------------END---------------- ------------TEST 1-------------- Test function one here ------------END---------------- ------------TEAR DOWN-------------- Disconnect to above created thing ------------END---------------- .------------SETUP-------------- Connect to above created thing ------------END---------------- ------------TEST 2-------------- Test function two here ------------END---------------- ------------TEAR DOWN-------------- Disconnect to above created thing ------------END---------------- .------------TEAR DOWN CLASS-------------- Remove whatever was created in setupclass -------------END---------------- ------------TEAR DOWN MODULE-------------- Destroy whatever you did in setUpModule ---------------------------------------------------------------------- Ran 2 tests in 0.004s OK
No comments:
Post a Comment