SHARE
TWEET

Untitled

a guest Sep 16th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Always write unit test cases in a file which starts with test_something.py
  2.  
  3. To run a test write on terminal
  4.  
  5. python -m unittest test_something.py
  6.  
  7. in class test_something.py define a class and inherit from unittest.TestCase class ClassName(object):
  8.     """docstring for ClassName"""
  9.     def __init__(self, arg):
  10.         super(ClassName, self).__init__()
  11.         self.arg = arg
  12.        
  13.  
  14. write the main function in the file
  15.  
  16. if '__name__' = '__main__':
  17.         unittest.main()
  18.  
  19. and then run
  20.  
  21. python test_something.py
  22.  
  23. both will give some result like:
  24.  
  25. Ran 1 test in 0.000s
  26.  
  27. write all function names like test_something otherwise that test function will not run
  28.  
  29.  
  30. setUp()
  31. and
  32. tearDown() methods run before and after of each test if defined
  33.  
  34.  
  35. to run something before every test case
  36. and to run something after every test case run:
  37.  
  38. setUpClass
  39. and tearDownClass classmethod
  40.  
  41.  
  42. from unittest.mock import patch
  43.  
  44. use patch as derocator or context manager
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top