I am Python newbie.
Is that possible to test Python script without wrapping code in functions / classes?
Let's say I want to cover with UTs this script https://github.com/aws-samples/aws-glue-samples/blob/master/examples/join_and_relationalize.py
Is that possible to write some UT https://docs.python.org/3/library/unittest.html for it ?
The issue is: I can not run methods/functions in AWS Glue but only script is enter point for that Framework.
https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-python.html
Is that possible to test Python script without wrapping code in functions / classes?
You can just create unit tests which run the script itself (using subprocess
for instance, checking that it has the correct retval / output).
The issue is: I can not run methods/functions in AWS Glue but only script is enter point for that Framework.
That doesn't actually preclude writing functions (or even classes) unless AWS Glue specifically forbids doing so (which I'd find rather unlikely).
It's rather common for Python files to be both runnable scripts and importable libraries. You just need to "gate" the script entry point:
def a_function():
print("a function")
def main():
a_function()
# magic!
if __name__ == '__main__':
main()
The last two lines are the magic bits: __name__
is only equal to "__main__"
if the file is being run as a script.
This means if you run the above file it'll immediately print "a function", but if you import
it, it won't do anything until you call a_function()
(or main()
).