Execute pytest from another python script within docker

Question:

I have a test script written using pytest and it works fine when I execute it as standalone.

User:~my_workspace/python_project$ pytest path_to_script/my_script.py

Now I have to run this script with different parameters and hence I have created a python file (script_to_invoke_pytest.py) so that I can run the pytest script from this python file.

I tried below approaches within my script_to_invoke_pytest.py

#approach 1
import subprocess
subprocess.Popen(['pytest', r'path_to_script/my_script.py'], shell=True)
#approach 2
import pytest
pytest.main([r'path_to_script/my_script.py'])

Both the approaches didn’t work and I get errors related no modules named xyz.

Im using pycharm ide and before running pytest, I execute the docker.

Please let me know how can I invoke my pytest from my python script script_to_invoke_pytest.py

Asked By: deepu

||

Answers:

In your second approach, you’re not telling python where to find the tests. Try:

import pytest
pytest.main([r'path_to_script/my_script.py'])
Answered By: Zephyrus