简体   繁体   English

具有Python异步功能的RobotFramework

[英]RobotFramework with Python's asyncio

I'm trying to run RobotFramework with Python3.6's asyncio. 我正在尝试使用Python3.6的asyncio运行RobotFramework。

The relevant Python-Code looks as follows: 相关的Python代码如下所示:

""" SampleProtTest.py """

import asyncio
import threading

class SubscriberClientProtocol(asyncio.Protocol):
    """
    Generic, Asynchronous protocol that allows sending using a synchronous accessible queue
    Based on http://stackoverflow.com/a/30940625/4150378
    """
    def __init__(self, loop):
        self.loop = loop

    """ Functions follow for reading... """


class PropHost:
    def __init__(self, ip: str, port: int = 50505) -> None:
        self.loop = asyncio.get_event_loop()
        self.__coro = self.loop.create_connection(lambda: SubscriberClientProtocol(self.loop), ip, port)
        _, self.__proto = self.loop.run_until_complete(self.__coro)
        # run the asyncio-loop in background thread
        threading.Thread(target=self.runfunc).start()

    def runfunc(self) -> None:
        self.loop.run_forever()

    def dosomething(self):
        print("I'm doing something")


class SampleProtTest(object):
    def __init__(self, ip='127.0.0.1', port=8000):
        self._myhost = PropHost(ip, port)

    def do_something(self):
        self._myhost.dosomething()

if __name__=="__main__":
    tester = SampleProtTest()
    tester.do_something()

If I run this file in python, it prints, as expected: 如果我在python中运行此文件,则会按预期进行打印:

I'm doing something

To run the code in Robot-Framework, I wrote the following .robot file: 为了在Robot-Framework中运行代码,我编写了以下.robot文件:

*** Settings ***
Documentation     Just A Sample
Library           SampleProtTest.py
*** Test Cases ***
Do anything
    do_something

But if I run this .robot-file, I get the following error: 但是,如果运行此.robot文件,则会出现以下错误:

Initializing test library 'SampleProtTest' with no arguments failed: This event loop is already running
Traceback (most recent call last):
  File "SampleProtTest.py", line 34, in __init__
    self._myhost = PropHost(ip, port)
  File "SampleProtTest.py", line 21, in __init__
    _, self.__proto = self.loop.run_until_complete(self.__coro)
  File "appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 408, in run_forever
    raise RuntimeError('This event loop is already running')

Can someone explain to me why or how I can get around this? 有人可以向我解释为什么或如何解决这个问题吗?

Thank you very much! 非常感谢你!

EDIT 编辑

Thanks to @Dandekar I added some Debug outputs, see code above, and get the following output from robot: 感谢@Dandekar,我添加了一些Debug输出,请参见上面的代码,并从robot获得以下输出:

- Loop until complete...
- Starting Thread...
- Running in thread...
==============================================================================
Sample :: Just A Sample                                                       
==============================================================================
Do anything                                                           - Loop until complete...
| FAIL |
Initializing test library 'SampleProtTest' with no arguments failed: This event loop is already running
Traceback (most recent call last):
  File "C:\share\TestAutomation\SampleProtTest.py", line 42, in __init__
    self._myhost = PropHost(ip, port)
  File "C:\share\TestAutomation\SampleProtTest.py", line 24, in __init__
    _, self.__proto = self.loop.run_until_complete(self.__coro)
  File "c:\users\muechr\appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "c:\users\muechr\appdata\local\programs\python\python36\lib\asyncio\base_events.py", line 408, in run_forever
    raise RuntimeError('This event loop is already running')
------------------------------------------------------------------------------
Sample :: Just A Sample                                               | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output:  C:\share\TestAutomation\results\output.xml
Log:     C:\share\TestAutomation\results\log.html
Report:  C:\share\TestAutomation\results\report.html

As I see it, the problem is that it already started the Thread BEFORE the test case. 如我所见,问题在于它已经在测试用例之前启动了线程。 Oddly, if I remove the line 奇怪的是,如果我删除行

_, self.__proto = self.loop.run_until_complete(self.__coro)

It seems to run through - but I can't explain why... But this is not a practical solution as I'm not able to access __proto like this... 它似乎一直存在-但我无法解释原因...但这不是一个实际的解决方案,因为我无法像这样访问__proto ...

Edit: Comment out the part where your code runs at start 编辑:注释掉您的代码在开始时运行的部分

# if __name__=="__main__":
#    tester = SampleProtTest()
#    tester.do_something()

That piece gets run when you import your script in robot framework (causing the port to be occupied). 当您在机械手框架中导入脚本时,该部分就会运行(导致占用端口)。

Also: If you are simply trying to run keywords asynchronously, there is a library that does that (although I have not tried it myself). 另外:如果您只是尝试异步运行关键字,那么有一个库可以做到这一点(尽管我自己还没有尝试过)。

robotframework-async robotframework -异步

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

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