python 例子: 实现动态加载类,并自动执行它定义的函数

=========

MiniJenkinsWorker2.py 内容如下

===========


'''

@author: hongbin
'''
import xmlrunner
import importlib
import os
import types
def main():
    '''
     Function: give one module name, this code will find those class prefixed with "myUT_"
               and run their functions whose name is prefixed with 'test_' automatically
               Here, in this module, you can add as many class as possible as long as their name
               convenctions are followed.
    2016-1-8
    '''  
    module=importlib.import_module("MyTestCase")
    module_dir=dir(module)
    #print("module content %s"%module_dir)
    for element in module_dir:
               if(isinstance(element, object) and (element.startswith('myUT_'))):
                  print("%s is class"%(element))
                  TestSuitClass = getattr(module, element)
                  
                  #instantiate an object
                  obj=TestSuitClass()
                  #根据一个类,去找它的以“test_”开头的成员函数,并自动执行
                  #get function list from a class
                  func_names=dir(TestSuitClass)
                  for func_name in func_names:
                      if func_name.startswith('test_'):
                          print("run func %s automatically"%func_name)
                          #get function
                          func=getattr(obj,func_name)
                          #call this function
                          func()
    
          
    return


if __name__== '__main__':

        main()


============

MyTestCase.py 内容如下

==============


import unittest
import xmlrunner
import os
import re
import time
from unittest.case import TestCase


def start():
      print("myfunc() is called")


class myUT_case5(TestCase):
        def setUp(self):
                pass
        def tearDown(self):
                pass


        def test_abs(self):
                print("myUT_case5.test_abs is called")
                #raise Exception("this case fail")
                
        def test_price(self):
                print("myUT_case5.test_price is called")
                
        def test_quantity(self):
                print("myUT_case5.test_quantity is called")
                                
        def _private(self):
                print("myUT_case5._private is called")
                
class myUT_case1(TestCase):
        def setUp(self):
                pass
        def tearDown(self):
                pass


        def test_name(self):
                print("myUT_case1.test_name is called")
                #raise Exception("this case fail")
                
        def test_getlocation(self):
                print("myUT_case1.test_getlocation is called")
                
        def test_getID(self):
              print("myUT_case1.test_getID is called")
                
        def _private(self):
                print("myUT_case1._private is called")


class myUT_case2(TestCase):
        def setUp(self):
                pass
        def tearDown(self):
                pass
            
        def test_calculate(self):
                print("myUT_case2.test_Calculate is called")
                #raise Exception("this case fail")
           
        def test_func1(self):
                print("myUT_case2.test_func1 is called")
                
        def test_abc(self):
                print("myUT_case2.test_abc is called")
                
        def _private(self):
                print("myUT_case2._private is called")


        
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值