【问题标题】:Is there a way to set up pytest to run a webapp2 server for coverage reports?有没有办法设置 pytest 来运行 webapp2 服务器以获取覆盖率报告?
【发布时间】:2017-03-23 18:00:17
【问题描述】:
我目前正在为 webapp2 服务器设置自动化测试,我想获得一份覆盖率报告。我正在使用适用于本地代码的 pytest 和 pytest-coverage,但问题是我正在向服务器发出请求(在本地运行)并且服务器运行的代码未包含在覆盖率报告中。有没有办法设置 pytest 来运行服务器本身,以便我可以在覆盖率报告中包含处理程序等?
【问题讨论】:
标签:
python
google-app-engine
code-coverage
pytest
【解决方案1】:
如果有人遇到类似情况,可以在这里使用pytest-xprocess。您可以编写如下所示的简单夹具来启动服务器实例并使其在测试中可用:
# content of conftest.py
import pytest
from xprocess import ProcessStarter
@pytest.fixture
def myserver(xprocess):
class Starter(ProcessStarter):
# startup pattern
pattern = "PATTERN"
# command to start process
args = ['command', 'arg1', 'arg2']
# ensure process is running and return its logfile
logfile = xprocess.ensure("myserver", Starter)
conn = # create a connection or url/port info to the server
yield conn
# clean up whole process tree afterwards
xprocess.getinfo("myserver").terminate()