您的位置:首页 > 编程语言 > Python开发

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

2016-01-08 17:16 781 查看
=========

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

        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: