繁体   English   中英

如何将 thrift 服务器或 TCP 服务器作为 pytest 夹具运行?

[英]How can I run a thrift server or TCP server as a pytest fixture?

平台 linux2,python 2.7.12-final-0

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

我一直在尝试将 Thrift 库 TCP 服务器作为 pytest 中的会话范围夹具运行几天。 我已经尝试将服务器作为后台线程运行,以便我的测试不会被阻止运行。

@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

这是一个我没有找到很好的文档或支持的问题,所以我不得不问这个问题..如何将 TCP 服务器设置为我的测试可以使用的 pytest 固定装置?

作为推论,在这种情况下我应该自己动手还是有一个很好的插件来支持这个 pytest 用例? 再一次,我的搜索结果很少。

我能够使用以下设置将 Thrift 服务器设置为 pytest 夹具:

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 用例? 再一次,我的搜索结果很少。

如果有人遇到同样的情况, 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()

暂无
暂无

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

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