Skip to main content

Creating Test Data: How to call Python Function from Java


For my testing purposes, I need to create an e-mail before running automated test cases so that it should used it for new registration and a new one for sending invitation to unregistered e-mail and there are some other test cases which use new e-mail for testing. I have create Python function which produce a new e-mail address in this format: gunesmess+1@gmail.com and next run it will create gunesmes+2@gmail.com and for every run, it creates a new one just increasing the last used e-mail number. You can use this file for your testing too. Just create numberRun.txt file in you directory.


For my recent project, I need to write my automated test cases in Java so I have learnt some Java but not as professional. For this project again I need to create new unregistered e-mail address for using almost the same scenarios. Therefore I needed to call Python function in my Java code, instead of writing it in Java. The following code is used for calling:

What you need is that you must install Jython which is Java-Python interpreter from here and then import the PythonInterpreter and just execute test_data.py with 
python.execfile("D:\\workspace\\pojectname\\src\\com\\base\\test_data.py");
the result should be return as Python object so you need to change it to Java string. Then you can use it.