unittest sample

python unittest sample

  • how to make test case

    • how to test function

    <!-- lang:python -->

      	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 -->

     	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 it

    • run in test case file

    <!-- lang:python -->

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

      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.TestLoader

      <!-- lang:python -->

        # 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 path

      use below

      <!-- 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
    

转载于:https://my.oschina.net/u/2293714/blog/361119

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值