-1

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

deepu
  • 147
  • 2
  • 12

1 Answers1

1

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'])
Zephyrus
  • 366
  • 1
  • 10
  • Sorry , I updated my second approach. I tried it , but I get Import errors such as no module named XYZ and so on. These modules loads and works fine , when I run the pytest as standalone. – deepu Jan 11 '23 at 13:08
  • Thanks. After I placed the script_to_invoke_pytest.py in the same folder of the pytest script. The solution worked. – deepu Jan 11 '23 at 13:16