简体   繁体   English

带有Pytest的高速公路Python RPC

[英]Autobahn Python RPC with Pytest

So in the application i have to test RPC call ( there is a webserver made with Autobahn Python ). 因此,在应用程序中,我必须测试RPC调用(有一个由Autobahn Python制成的Web服务器)。 I wanna do integration test on those. 我想对它们进行集成测试。 So I am using Pytest. 所以我在用Pytest。 The way i do it is like that : 1) I have a test class like this : 我这样做的方式是这样的:1)我有一个像这样的测试类:

@pytest.mark.incremental
class TestCreateUser:

    @pytest.fixture(scope="function", params="Json Test data")
    def create_user_data(self, request):
        return request.param

    def test_valid_user_creation(self, create_user_data):  
        ws_realm = config["websocket"]["realm"]

        runner = ApplicationRunner(
            url="%s://%s:%s/ws" % (
            config["websocket"]["protocol"], config["websocket"]["host"], config["websocket"]["port"]),
            realm=ws_realm,
            extra={"method": u"appname.create_user", "email": create_user_data["username"], create_user_data["password"]}
        )
        runner.run(MyComponent)

Then i have a component called "MyComponent" : 然后,我有一个名为“ MyComponent”的组件:

def __init__(self, config=None):
        super().__init__(config)

    @asyncio.coroutine
    def onJoin(self, details):
        try:
            result = yield from self.call(self.config.extra["method"], self.config.extra["email"], self.config.extra["password"])
        except Exception as e:
            print("Error: {}".format(e))
        else:
            print(result)

        self.leave()

    def onDisconnect(self):
        asyncio.get_event_loop().stop()

So i have a few problems with that, first i cannot get the result of the RPC call in the test class and second when i ran multiple times "test_valid_user_creation" ( with the fixture system ) i got an error like this : 所以我有一些问题,首先,我无法在测试类中获得RPC调用的结果,其次,当我多次运行“ test_valid_user_creation”(使用夹具系统)时,出现了这样的错误:

./../.pyenv/versions/novalibrary/lib/python3.4/site-packages/autobahn/asyncio/wamp.py:167: in run
    (transport, protocol) = loop.run_until_complete(coro)
../../.pyenv/versions/3.4.3/lib/python3.4/asyncio/base_events.py:293: in run_until_complete
    self._check_closed()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_UnixSelectorEventLoop running=False closed=True debug=False>

    def _check_closed(self):
        if self._closed:
>           raise RuntimeError('Event loop is closed')
E           RuntimeError: Event loop is closed

../../.pyenv/versions/3.4.3/lib/python3.4/asyncio/base_events.py:265: RuntimeError

So my question is How do i retrieve the value of the RPC call AND how can i not get the "Event loop is closed " when the test is called multiple time with the fixture. 所以我的问题是,当使用夹具多次调用测试时,如何获取RPC调用的值以及如何无法获得“事件循环已关闭”。

Thank you. 谢谢。

So basically, to return the value i create another module "shared_result" and i put one global variable in there to return the value from the component into the test class. 因此,基本上,要返回该值,我创建了另一个模块“ shared_result”,并在其中放置了一个全局变量,以将该值从组件返回到测试类中。 As for the "Event loop is closed" exception i was getting, i do that before each time i call the runner : 至于我收到的“事件循环已关闭”异常,我在每次致电跑步者之前都要这样做:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

This is where i find the solution : https://github.com/aio-libs/aiohttp/issues/441 这是我找到解决方案的地方: https : //github.com/aio-libs/aiohttp/issues/441

If somebody can comment on this and explain why this is needed and gave me a better solution, it would be great. 如果有人可以对此发表评论并解释为什么需要这样做并为我提供更好的解决方案,那就太好了。

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

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