Unit Testing

It is the first level software testing method, where a small part of the code has tests, this is used for the software quality and productivity. We have used white box testing for using the Unit testing.

Method

  • Test fixture : it is to create the baseline for running the test in a fixed environment in which tests are run or it is repeatable.
  • Test case : A test case is the set of condition which is used determined the check the condition under the system.
  • Test suits : test suit is the growing number of test cases,it needs for categories to increase as well. Test suit is similar as shelves, the group test case together.
  • Test runner : this test is done by the user in the defined time and along with their outcome.

Basic test structure

Implements unitesting

import unittest 
  
class BasicTest(unittest.TestCase): 
   
    def check(self):         
        self.assertTrue(True) 
  
if __name__ == '__main__': 
    unittest.main()

This is the basic code of using the unittest framework, which is having a single test. This test () unittest method fails if FALSE is returned instead of TRUE.

Output

C:\Users\ASUS\Desktop>python abc.py

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

C:\Users\ASUS\Desktop>

Outcome Possible

OK : its mean all the test is passed
FAIL : It is mean test is not passed and Assertion Error Raised
ERROR : this mean test cases raised the error

import unittest 
  
class Test_Method_String(unittest.TestCase): 
      
    def set(self): 
        pass
    def teststring_A(self): 
        self.assertEqual( 'A'*4, 'AAAA') 
  

    def TEST_UPPER(self):         
        self.assertEqual('example'.upper(), 'example') 
  

    def TEST_IS_UPPER(self):         
        self.assertTrue('EXAMPLE'.isupper()) 
        self.assertFalse('Example'.isupper()) 
  
 
    def TEST_STRIP(self):         
        string = 'salesforcedrillers'
        self.assertEqual(s.strip('sales'), 'forcedrillers') 
  

    def TEST_SPLIT(self):         
        s = 'SALES FORCE'
        self.assertEqual(s.split(), ['SALES', 'FORCE']) 
        with self.assertRaises(TypeError): 
            s.split(2) 
  
if __name__ == '__main__': 
    unittest.main() 

Output

Ran 1 test in 0.000s

OK

C:\Users\ASUS\Desktop>

In this program we have covered 5 different type string methods.

Basic Term which is Used in Program

assertEqual(): checking that the result is the same as expected.

assertTrue() / assertFalse() – this method is used to check that result is true or false
assertRaises() this method is used for raise the specific exception

Subscribe Now