unittest sample

python unittest sample

  • how to make test casepython

    • how to test function

    <!-- lang:python --> ui

    import unittest
      	import functions
    
    
      	class FunctionTestCase (unittest.TestCase):  
    
      		# preper for test, e.g. make file, objs, etc
      		def setUp(self):  
          		pass
    
          	# clean resource made in setUp
          	def tearDown(self):
          		pass
    
          	# MUST begin with test
          	def test_function1(self):
    
          		ret = functions.func1()
          		self.assertEqual(ret, expect, 'some msg when 
          						failed')
    • how to test class

    <!-- lang:python --> this

    import unittest
     	from module1 import Clz1
    
    
     	class Module1TestCase (unittest.TestCase):  
    
     		# preper for test, e.g. make file, objs, etc
      	def setUp(self):  
          	self.clz_obj = Clz1()
    
      	# clean resource made in setUp
          def tearDown(self):
          	pass
    
          def test_function1(self):
          	out = self.clz_obj.func1()
          	self.assertEqual(out, expect)
    
          def test_function2(self):
          	out = self.clz_obj.func2(args, kwagrs)
          	self.assertEqual(out, expect)
  • how to run itspa

    • run in test case file

    <!-- lang:python --> code

    if '__main__' == __name__:
      	unittest.main()
    • run regressionget

      if there are many test case, it must be actually. you want to a entrance to run all by automatically. you need unittest.TestSuite uniittest.TestLoaderit

      <!-- lang:python --> io

      # file: testmain.py
        import os, sys, unittest
      
        def finAllInCurrentDir():
        	path = 	os.path.abspath(os.path.dirname(sys.argv[0]))
            files = os.listdir(path)
            # end of "test.py"
            test = re.compile(".*test\.py$", re.IGNORECASE)
            files = filter(test.search, files)
            module_name_converter = lambda f: os.path.splitext(f)[0]
            module_names = map(module_name_converter, files)
            modules = map(__import__, module_names)
            loader = unittest.defaultTestLoader.loadTestsFromModule
            print unittest.defaultTestLoader.discover(path, "*test.py$")
            return unittest.TestSuite(map(loader, modules))
      
        if '__main__' == __name__:
        	unittest.main(defaultTest="finAllInCurrentDir")

      actually, test code should in test package, so if you run it directly, maybe not work, because do not find the module want to test, not in search pathfunction

      use belowclass

      <!-- lang:python -->

      python -m test.testmain

      it will be work

    • command line

    there is another way by python

    <!-- lang:python -->

    cd project_directory 
      python -m unittest discover
    
      -v, --verbose Verbose output
    
      -s, --start-directory directory Directory to start discovery (. default)
    
      -p, --pattern pattern Pattern to match test files (test*.py default)
    
      -t, --top-level-directory directory Top level directory of project (defaults to start directory)
    
      python -m unittest discover -s project_directory -p '*_test.py'
      python -m unittest discover project_directory '*_test.py'

    so, above can like this

    <!-- lang:python -->

    cd project_directory
      python -m unittest discover -p '*test.py' -s 'test_folder' -v
相關文章
相關標籤/搜索