【发布时间】:2018-11-16 11:15:38
【问题描述】:
我有以下问题。
我想在部署到生产环境之前在本地烧瓶服务器上运行测试。我为此使用pytest。我的 conftest.py 目前看起来像这样:
import pytest
from toolbox import Toolbox
import subprocess
def pytest_addoption(parser):
"""Add option to pass --testenv=local to pytest cli command"""
parser.addoption(
"--testenv", action="store", default="exodemo", help="my option: type1 or type2"
)
@pytest.fixture(scope="module")
def testenv(request):
return request.config.getoption("--testenv")
@pytest.fixture(scope="module")
def testurl(testenv):
if testenv == 'local':
return 'http://localhost:5000/'
else:
return 'https://api.domain.com/'
这允许我通过输入命令 pytest 来测试生产 api,并通过输入 pytest --testenv=local 来测试本地烧瓶服务器
此代码完美运行。
我的问题是每次我想像这样进行本地测试时,我都必须从终端手动实例化本地烧瓶服务器:
source ../pypyenv/bin/activate
python ../app.py
现在我想添加一个夹具,在测试开始时在后台启动一个终端,并在完成测试后关闭服务器。经过大量的研究和测试,我仍然无法让它工作。这是我添加到 conftest.py 中的行:
@pytest.fixture(scope="module", autouse=True)
def spinup(testenv):
if testenv == 'local':
cmd = ['../pypyenv/bin/python', '../app.py']
p = subprocess.Popen(cmd, shell=True)
yield
p.terminate()
else:
pass
我得到的错误来自请求包,它说没有连接/被拒绝。
E requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5000): 超过最大重试次数 带有网址:/登录(由 NewConnectionError(': 建立新连接失败: [Errno 111] 连接被拒绝',))
/usr/lib/python3/dist-packages/requests/adapters.py:437: 连接错误
这对我来说意味着 app.py 下的烧瓶服务器不在线。有什么建议么?我对更优雅的选择持开放态度
【问题讨论】:
标签: python-3.x flask subprocess pytest