1

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 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In what sense is that a test, there's no description of what's actually supposed to happen, no assertions.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.