从XML调用Python函数
我有一个带有lib_procs.py、main_test.py和test_engine.py的小测试程序。现在,在test_engine.py中实例化了main_test obj。obj用于调用main_test.py中的execute方法。在execute方法procs.run_cmdmsg中,调用cmd发送命令并返回消息 lib_procs.py从XML调用Python函数,python,xml,Python,Xml,我有一个带有lib_procs.py、main_test.py和test_engine.py的小测试程序。现在,在test_engine.py中实例化了main_test obj。obj用于调用main_test.py中的execute方法。在execute方法procs.run_cmdmsg中,调用cmd发送命令并返回消息 lib_procs.py def run_cmd(msg, cmd): #send hardware command and return something
def run_cmd(msg, cmd):
#send hardware command and return something
#it has other hardware specific method too
main_test.py
import lib_procs as procs
class main_test(object):
def __init__(self, xmlpath, name)
#initialize
def execute(self):
#Currently
#parse xml
msg = xml.msg
cmd = xml.cmd
output = procs.run_cmd(cmd, msg)
#Do other things too
测试引擎
if __name__ == "__main__::
test_obj = main_test(xmlpath, test_name)
test_obj.execute()
我想做的是能够通过XML文件运行run_cmd。XML文件应该包含关于调用哪个函数的信息。这应该在main_test.py中提取。这样做的目的是使测试框架具有通用性,其他人可以通过更改XML中的信息来使用该框架。现在procs.run\u cmd是特定于硬件的。有可能吗?当然有可能。可以使用exec,但最好是基于XML中的适当字段编写条件语句。或者,将函数名映射到实际函数。具体细节实际上取决于实现细节。@whrrgarbl我不太确定如何在xml标记中声明python方法和模块。有什么例子吗?好吧,这真的取决于你-这是实现细节部分。您想在XML文件中包含实际的python代码吗?或者只是命名要调用的函数?如果你想走那条路,可能会有用。