我正在为 IBM Cloud Code Engine 开发 Python 函数。在部署之前如何在本地测试它们?我的目标是加快开发周期,并且只有在验证了代码的功能后才进行部署。
您可以使用以下模式。将函数代码放在子目录 func 中,在父目录中使用此包装器代码 (wrapper.py):
# import the Code Engine function: func/__main__.py
from func.__main__ import main
import sys, json
if __name__ == "__main__":
# open file, read JSON config
with open(str(sys.argv[1])) as confFile:
params=json.load(confFile)
# invoke the CE function and print the result
print(main(params))
然后调用它
python wrapper.py params.json
。文件 params.json 将保存一个 JSON 对象,其参数和系统变量与代码引擎传递给函数的参数和系统变量相同。