Python 如何将thrift服务器或TCP服务器作为pytest设备运行?

Python 如何将thrift服务器或TCP服务器作为pytest设备运行?,python,pytest,thrift,coverage.py,pytest-cov,Python,Pytest,Thrift,Coverage.py,Pytest Cov,平台linux2、python 2.7.12-final-0 我的集成测试ping外部服务器,但我想将其更改为使用测试夹具 几天来,我一直在尝试在pytest中将Thrift library TCP服务器作为会话范围固定装置运行。我已经尝试将服务器作为后台线程运行,这样我的测试就不会被阻止运行 @pytest.fixture(scope="session", autouse=True) def thrift_server(): config = get_config() npro

平台linux2、python 2.7.12-final-0

我的集成测试ping外部服务器,但我想将其更改为使用测试夹具

几天来,我一直在尝试在pytest中将Thrift library TCP服务器作为会话范围固定装置运行。我已经尝试将服务器作为后台线程运行,这样我的测试就不会被阻止运行

@pytest.fixture(scope="session", autouse=True)
def thrift_server():
    config = get_config()
    nprocesses = 4

    try:
        nprocesses = config['num_processes']
    except:
        pass

    args = (Handler, config['db_credentials'], config['server_port'])
    kwargs = ({'env': config['env'], 'processpool': True, 'num_processes': nprocesses,
        'handler_config': config, 'logfile': 'tests/test_server_log.txt'})

    tserver = ThriftServer()
    tserver.add_path(config['thrift_path'])
    tserver.set_service("search")

    try:
        thread = threading.Thread(target=tserver.runserver, args=args, kwargs=kwargs)
        thread.daemon = True
        thread.start()
        yield tserver
    except:
        print("BOOHOO")
    print("TEARDOWN: test server")
对于我的每个测试,我都会从pytest库代码中得到一个错误:

ERROR at setup of Tests.test_item_is_found 
venv/lib/python2.7/site-packages/_pytest/runner.py:226: in from_call
    result = func()
venv/lib/python2.7/site-packages/_pytest/runner.py:198: in <lambda>
    lambda: ihook(item=item, **kwds), when=when, reraise=reraise
venv/lib/python2.7/site-packages/pluggy/hooks.py:289: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
venv/lib/python2.7/site-packages/pluggy/manager.py:87: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
venv/lib/python2.7/site-packages/pluggy/manager.py:81: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
venv/lib/python2.7/site-packages/_pytest/runner.py:116: in pytest_runtest_setup
    item.session._setupstate.prepare(item)
venv/lib/python2.7/site-packages/_pytest/runner.py:362: in prepare
    col.setup()
venv/lib/python2.7/site-packages/_pytest/unittest.py:119: in setup
    self._request._fillfixtures()
venv/lib/python2.7/site-packages/_pytest/fixtures.py:469: in _fillfixtures
    item.funcargs[argname] = self.getfixturevalue(argname)
venv/lib/python2.7/site-packages/_pytest/fixtures.py:479: in getfixturevalue
    return self._get_active_fixturedef(argname).cached_result[0]
venv/lib/python2.7/site-packages/_pytest/fixtures.py:502: in _get_active_fixturedef
    self._compute_fixture_value(fixturedef)
venv/lib/python2.7/site-packages/_pytest/fixtures.py:587: in _compute_fixture_value
    fixturedef.execute(request=subrequest)
venv/lib/python2.7/site-packages/_pytest/fixtures.py:894: in execute
    return hook.pytest_fixture_setup(fixturedef=self, request=request)
venv/lib/python2.7/site-packages/pluggy/hooks.py:289: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
venv/lib/python2.7/site-packages/pluggy/manager.py:87: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
venv/lib/python2.7/site-packages/pluggy/manager.py:81: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
venv/lib/python2.7/site-packages/_pytest/fixtures.py:936: in pytest_fixture_setup
    result = call_fixture_func(fixturefunc, request, kwargs)
venv/lib/python2.7/site-packages/_pytest/fixtures.py:791: in call_fixture_func
    res = next(it)
E   StopIteration
设置测试时出错。找到测试项目 venv/lib/python2.7/site-packages/\u-pytest/runner.py:226:in-from\u调用 结果=func() venv/lib/python2.7/site-packages/_-pytest/runner.py:198:in lambda:ihook(项目=项目,**kwds),when=when,reraise=reraise venv/lib/python2.7/site-packages/pluggy/hooks.py:289:in\uu调用__ 返回self._hookexec(self,self.get_hookimpls(),kwargs) venv/lib/python2.7/site-packages/pluggy/manager.py:87:in\uhookexec 返回self.\u inner\u hookexec(钩子、方法、kwargs) venv/lib/python2.7/site-packages/pluggy/manager.py:81:in firstresult=hook.spec.opts.get(“firstresult”),如果hook.spec else为False, venv/lib/python2.7/site-packages/\u-pytest/runner.py:116:在pytest\u-runtest\u设置中 项目。会话。\u设置状态。准备(项目) venv/lib/python2.7/site packages/_-pytest/runner.py:362:准备中 col.setup() venv/lib/python2.7/site-packages/_-pytest/unittest.py:119:设置中 self.\u请求。\u fillfixtures() venv/lib/python2.7/site-packages/_-pytest/fixtures.py:469:in_-fillfixtures item.funcargs[argname]=self.getfixturevalue(argname) venv/lib/python2.7/site packages/_pytest/fixtures.py:479:in-getfixturevalue 返回self.\u获取\u活动\u修复定义(argname).cached\u结果[0] venv/lib/python2.7/site packages/_-pytest/fixtures.py:502:in\u-get\u-active\u-fixturedef 自计算夹具值(fixturedef) venv/lib/python2.7/site packages/\u pytest/fixtures.py:587:in\u compute\u fixture\u值 执行(请求=子请求) venv/lib/python2.7/site-packages/_-pytest/fixtures.py:894:in-execute 返回hook.pytest\u fixture\u设置(fixturedef=self,request=request) venv/lib/python2.7/site-packages/pluggy/hooks.py:289:in\uu调用__ 返回self._hookexec(self,self.get_hookimpls(),kwargs) venv/lib/python2.7/site-packages/pluggy/manager.py:87:in\uhookexec 返回self.\u inner\u hookexec(钩子、方法、kwargs) venv/lib/python2.7/site-packages/pluggy/manager.py:81:in firstresult=hook.spec.opts.get(“firstresult”),如果hook.spec else为False, venv/lib/python2.7/site packages/\u pytest/fixtures.py:936:在pytest\u fixture\u设置中 结果=调用\u fixture\u func(fixturefunc、request、kwargs) venv/lib/python2.7/site packages/_-pytest/fixtures.py:791:in-call_-fixture_-func res=下一个(it) 停止迭代 这是一个我还没有找到很好的文档或支持的问题,因此我不得不问一个问题..如何将TCP服务器设置为我的测试可以使用的pytest设备


作为推论,这是一种我应该自己动手的情况,还是有一个好的插件来支持这个pytest用例?同样,我的搜索结果也很少。我能够使用以下设置将Thrift服务器设置为pytest fixture:

import pytest
import threading
from pythrift import ThriftServer
from myHandler import Handler


@pytest.fixture(scope="session", autouse=True)
def thrift_server():
     config = get_config()
     nprocesses = 4

     try:
         nprocesses = config['num_processes']
     except:
         pass

     args = (Handler, config['db_credentials'], config['server_port'])
     kwargs = ({'env': config['env'], 'processpool': True, 'num_processes': nprocesses,
         'handler_config': config, 'logfile': 'tests/test_server_log.txt'})

     tserver = ThriftServer()
     tserver.add_path(config['thrift_path'])
     tserver.set_service("search")

     try:
         thread = threading.Thread(target=tserver.run_server, args=args, kwargs=kwargs)
         thread.daemon = True
         thread.start()
         yield tserver
     except Exception as e:
         print(e)
     print("TEARDOWN: test server")
作为推论,这是一种我应该自己滚的情况,还是 有一个好的插件来支持这个pytest用例吗?再说一遍,我的搜索 在这件事上几乎没有发现

如果有人遇到同样的情况,这里应该有用。您可以编写一个简单的fixture,如下所示,以启动服务器实例并使其在测试中可用:

# 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()

它不应该是yieldtserver?您似乎没有在任何地方声明
server
。是的,在我的代码中是yield-tserver,我编辑了postAside,在代码中没有看到任何明显的错误-我想
yield-tserver
行没有执行(这是错误消息告诉您的)。可能
thread.start()
引发了一个异常,该异常在
中被捕获,除了:
。尝试运行
pytest-s
并检查是否打印了
BOOHOO