订阅以获取消息,这些消息在Robot Framework MQTT库中不起作用

时间:2018-12-03 08:25:32

标签: robotframework mqtt mosquitto

我是Robot框架和MQTT的新手。我使用mosquitto调出了订户/发布者设置。我编写了代码以发布到订阅的主题。发布代码运行正常,我可以在订阅者窗口中看到输出。但是,subscribe关键字对我不起作用。

我尝试了“订阅”,“订阅”和“验证”。对于前者,我得到Messages = [],对于后者,“预期的有效负载没有到达主题”。代码的屏幕快照已随附。

Subscribe and get messages
    Subscribe   MQTTtest2   qos=2   timeout=1   limit=0
    I Publish an MQTT request
    ${messages}=    Subscribe   MQTTtest2   qos=2   timeout=5   limit=0
    log to console  Messages=${messages}

I Publish an MQTT request
    connect   127.0.0.1
    publish  MQTTtest2   testmessage  2  ${false}
    disconnect

MQTT_SUBSCRIBE_AND_VALIDATE
[Tags]  mqtt
[Setup]     Connect     127.0.0.1
I SUBSCRIBE to a Topic
[Teardown]  Disconnect

I SUBSCRIBE to a Topic
#connect  ${MQTT.hostname}
subscribe and validate  ${MQTT.topic}  ${MQTT.qos}  ${MQTT.message}  5
#disconnect

可以让我知道我做错了什么吗? Subscribe Publish

2 个答案:

答案 0 :(得分:0)

在进行订阅操作的情况下,也必须使用Connect关键字连接到代理。

当前,您尚未在任何Subscribe关键字之前建立连接。在您的I Publish an MQTT request中,您进行连接以使其成功,但同时也进行了断开连接,因此下一个订阅再次失败。

如果您这样修改测试:

*** Keywords ***
Subscribe And Get Messages
    Subscribe   topic=MQTTtest2   qos=2   timeout=5   limit=0
    I Publish An MQTT Request
    ${messages}=    Subscribe   topic=MQTTtest2   qos=2   timeout=5   limit=0
    Log To Console  Messages=${messages}

I Publish An MQTT Request
    Publish  topic=MQTTtest2   testmessage  2  ${false}

*** Test Cases ***
MQTT test
    [Tags]  mqtt
    [Setup]     Connect     127.0.0.1
    Subscribe And Get Messages
    [Teardown]  Disconnect

它应该正常工作:

==============================================================================
Robot Framework.My Test
==============================================================================
MQTT test                                                             .Messages=[b'testmessage']
MQTT test                                                             | PASS |

而且,MQTT库似乎在每个Connect中都注册为新客户端,因此,如果对每个订阅和发布分别进行连接/断开连接,则可能对您不起作用。

答案 1 :(得分:0)

Subscribe and Validate似乎正在工作。

    *** Variable ***
${mqtt-server}   127.0.0.1
${mqtt-path}    ABCD
${message-payload}  MQTT-Test-2.robot payload1 test message from RobotFramework
${message-payload2}  MQTT-Test-2.robot payload2 test message from RobotFramework

*** Keywords ***
Subscribe And Get Messages
    Subscribe   topic=${mqtt-path}  qos=2   timeout=5   limit=0
    #I Publish An MQTT Request
    Publish     topic=${mqtt-path}    message=${message-payload}
    ${messages}=    Subscribe    topic=${mqtt-path}   qos=1   timeout=5   limit=0
    Log To Console  Messages=${messages}

Subscribe And Validate Messages
    Subscribe   topic=${mqtt-path}  qos=2   timeout=5   limit=0
    #I Publish An MQTT Request
    Publish     topic=${mqtt-path}    message=${message-payload2}    
    ${messages}=    Subscribe And Validate    topic=${mqtt-path}   qos=1   timeout=5    payload=${message-payload2}
    Log To Console  Messages=${messages}    

Publish An MQTT Request
    Publish     topic=${mqtt-path}    message=${message-payload}

*** Test Cases ***
MQTT test
    [Tags]  mqtt
    [Setup]  Connect    127.0.0.1
    Publish An MQTT Request
    Subscribe And Get Messages
    Subscribe And Validate Messages
    [Teardown]  Disconnect

================================================ ==============================

MQTT-Test-3 ::这是一个MQTT测试项目:探索...的MQTT-Test-2.robot ...

MQTT测试..Messages = ['来自RobotFramework的MQTT-Test-2.robot负载1测试消息'] .Messages = None

MQTT测试|通过|

MQTT-Test-3 ::这是一个MQTT测试项目:MQTT-Test-2.robot可以...通行证| 1个关键测试,1个通过,0个失败

总共1个测试,1个通过,0个失败