For my unittests I use a setup which is executed in a main.py
in the top directory which is executed with several parameters:
python3.9 main.py -i f -f Schematic.xml -d Inno -p Testrun -x
How do I execute that correctly in the setUp of my unittests?
I am aware this question has been asked often already but I can't get this to work. Most of the solutions suggested on SO just make my unittests get stuck or result in an error.
This one is the closest I could bring it to work but the tests just get stuck and I manually need to continue them:
subprocess.Popen(shlex.split("python main.py -i f -f Schematic.xml -d Inno -p Testrun -x"), stdout=open("cmd.log", 'ab'))
What I don't understand is if there is an input to be or what is expected. I took the solution from subprocess.Popen shell=True to shell=False as the previous variant would only get stuck:
subprocess.Popen("python main.py -i f -f Schematic.xml -d Inno -p Testrun -x", shell=True)
Apparently setting shell=False
alone is not possible, so I followed the instructions and modified my code (https://stackoverflow.com/a/25465793/2516892). If I just set it to false, I only get an error:
FileNotFoundError: [Errno 2] No such file or directory: 'python main.py -i f -f Schematic.xml -d Inno -p Testrun -x'
So the correct way is supposed to be:
subprocess.check_call(['main.py', '-i f -f Schematic.xml -d Inno -p Testrun -x'], shell=False)
But again all I get is an error:
File "/usr/lib64/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'main.py
Apparently os
shouldn't be used any more. Also it doesn't work:
os.system('python main.py -i f -f Schematic.xml -d Inno -p Testrun -x')
Here I'm getting:
sh: main.py: command not found
Edit: It is now "solved", but the solution is not really what I like as I consider it as cheap and doesn't answer the question - why the previous trials didn't work. Despite me being in the home directory just calling main.py that didn't work. I used the absolute path:
os.system('python /home/docker/main.py -i f -f Schematic.xml -d Inno -p Testrun -x')
I will keep it like this for now, but again. This is not really what I had in mind