I am developing Python functions for IBM Cloud Code Engine and deploy them from local code. How can I test them locally before deploying them? My goal is to speed up the development cycle and only deploy once I have verified the code's basic functionality.
1 Answer
You could use the following pattern. With your actual function code in a subdirectory func, use this wrapper code (wrapper.py) in the parent directory:
# 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))
Then invoke it python wrapper.py params.json. The file params.json would hold a JSON object with the same parameters and system variables as it would be passed to the function by Code Engine. You could then check the output (here just the printed) against whatever you need.
2 Comments
jonrsharpe
In what sense is that a test, there's no description of what's actually supposed to happen, no assertions.
data_henrik
That's the skeleton to invoke the FaaS function locally and pass the parameters. Whatever is needed for unit testing, checking the output for example, could be there. Without that, you would just deploy the function. Do you have a better solution? How do you test Code Engine functions? @jonrsharpe