如何仅修改Folder5,在Robot Framework中调用helperAA.py的所有函数?
需求可行,附实现方案
完全可以实现你的需求,所有改动只需要在Folder5下的helperBB.py里完成,不用碰其他目录的文件。具体操作如下:
- 在
helperBB.py的最顶部添加一段代码,把根目录(也就是Folder1和Folder2所在的目录)加入Python的模块搜索路径:
import sys from pathlib import Path # 逐级向上找到根目录:当前脚本所在Folder5 → 往上四级就是根目录 root_path = Path(__file__).parent.parent.parent.parent.parent sys.path.append(str(root_path))
- 接着直接写你想要的导入语句,再定义自己的
func3即可:
from helperAA import * # 这里写你自己的func3逻辑,会覆盖从helperAA导入的同名函数 def func3(): # 自定义实现 print("这是helperBB的func3")
这样一来:
helperBB.py会批量导入helperAA.py里的所有函数(包括func1、func2),同时保留自己的func3;- 在
Test.robot里只需要引用helperBB.py,就能直接调用func1、func2,和调用helperBB自己的函数完全一样。
内容的提问来源于stack exchange,提问作者Yogendiran Barath
相关产品推荐
相关产品推荐

