简体   繁体   中英

Execute pytest from another python script within docker

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

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM