简体   繁体   English

从 docker 中的另一个 python 脚本执行 pytest

[英]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.我有一个使用 pytest 编写的测试脚本,当我独立执行它时它工作正常。

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.现在我必须使用不同的参数运行这个脚本,因此我创建了一个 python 文件(script_to_invoke_pytest.py) ,这样我就可以从这个 python 文件运行 pytest 脚本。

I tried below approaches within my script_to_invoke_pytest.py我在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.这两种方法都不起作用,我收到与没有名为 xyz 的模块相关的错误。

Im using pycharm ide and before running pytest, I execute the docker.我使用 pycharm ide,在运行 pytest 之前,我执行了 docker。

Please let me know how can I invoke my pytest from my python script script_to_invoke_pytest.py请让我知道如何从我的 python 脚本 script_to_invoke_pytest.py 调用我的 pytest

In your second approach, you're not telling python where to find the tests.在您的第二种方法中,您没有告诉 python 在哪里可以找到测试。 Try:尝试:

import pytest
pytest.main([r'path_to_script/my_script.py'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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