我们知道JAVA 有反射 和 动态加载技术:比如给一个class名字,则JVM可以动态加载这个类,然后通过JAVA API知道它的方法,然后去调用和执行这些方法。
而PYTHON是Java写的,按此推论,PYTHON有这样的技术吗?自动化测试是否可能的吗? 答案是:YES.
说明: 运行Main.py,它会去动态加载MyTestCase,然后依次加载该module里面的类,然后执行这些类里的方法。达到自动测试运行的目的。
#运行输出结果是
>>>
__init__() called
__call__() called
ShowName called
myUT_case1 is class
run func test_getID automatically
myUT_case1.test_getID is called
run func test_getlocation automatically
myUT_case1.test_getlocation is called
run func test_name automatically
myUT_case1.test_name is called
myUT_case2 is class
run func test_abc automatically
myUT_case2.test_abc is called
run func test_calculate automatically
myUT_case2.test_Calculate is called
run func test_func1 automatically
myUT_case2.test_func1 is called
myUT_case5 is class
run func test_abs automatically
myUT_case5.test_abs is called
run func test_price automatically
myUT_case5.test_price is called
run func test_quantity automatically
myUT_case5.test_quantity is called
>>>
源代码如下:
Main.py 代码 (先写好)
===============
'''
@author: hongbin
'''
import xmlrunner
import importlib
import os
import types
class DemoOverride():
def __init__(self):
print("__init__() called")
self.name="noname"
def __call__(self,id,name):
print("__call__() called")
return self.ShowName()
def ShowName(self):
print("ShowName called")
return self.name
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
'''
mInstance=DemoOverride()
mInstance(1,4)
#dynamically load python module named MyTestCase
module=importlib.import_module("MyTestCase")
module_dir=dir(module)
#print("module content %s"%module_dir)
#find those more than 1 UT classes named 'myUT_'
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 member function list from this 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)
#run this function
func()
return
if __name__== '__main__':
main()
MyTestCase.py (MyTestCase.py 后来才写的,也就是main.py写好后,它肯定不知道MyTestCase里面是啥内容哈)
=========
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")