我正在尝试编写一个测试用例,以查看我的操作是否正在传输到ActiveMQ主题中。 我在pytest之外检查了我的代码,以某种方式代码可以顺利运行,并且可以看到我的消息在ActiveMQ主题上生成了一个事件。但是,如果我在pytest案例中运行它,我将无法捕获事件。我似乎找不到原因。我们正在设置接收器,但是如果使用pytest,则不会获得标题列表。外部pytest可以正常工作。
没有pytest的版本:
import messenger
import client
import time
import amq_client as aq
lst=aq.MyListener(aq.conn)
aq.conn.set_listener('', lst)
aq.conn.start()
aq.conn.connect(login=aq.user, passcode=aq.password)
aq.conn.subscribe(destination=aq.destination, ack='auto', id=1234)
print("Waiting for messages...")
resp=messenger.Messenger().send_message()
print(resp)
lognum=resp[1].split("=")[1]
time.sleep(10)
actions = [x['ENTITY_ACTION'] for x in lst.header_list]
accounts = [x['ACCOUNT'] for x in lst.header_list]
print('actions ', actions)
print(' ')
print(' ')
print(' ')
print(' ')
print('ACCOUNTS', accounts)
if(str(lognum) in accounts and 'NEW_ACCOUNT' in actions ):
print('By this it WORKS')
输出:
actions ['NEW_ACCOUNT', 'ACCOUNT_GROUP_CHANGED']
ACCOUNTS ['720308353', '720308353']
By this it WORKS
Pytest版本:
import pytest
import messenger
import amq_client as aq
import time
class Test_AmqTestCase(object):
def test_newaccount(self):
lst=aq.MyListener(aq.conn)
aq.conn.set_listener('', lst)
aq.conn.start()
aq.conn.connect(login=aq.user, passcode=aq.password)
aq.conn.subscribe(destination=aq.destination, ack='auto', id=1234)
print("Waiting for messages...")
resp=messenger.Messenger().send_message()
print(resp)
lognum=resp[1].split("=")[1]
t_end=time.time()+30
t_start=time.time()
while time.time() < t_end:
time.sleep(1)
t_total=time.time()-t_start
print('Total time for event occur :', t_total)
print(' ')
actions = [x['ENTITY_ACTION'] for x in lst.header_list]
accounts = [x['ACCOUNT'] for x in lst.header_list]
print('ACCOUNTS', accounts)
assert (str(lognum) in accounts and 'NEW_ACCOUNT' in actions)
pytest结果:
assert (str(lognum) in accounts and 'NEW_ACCOUNT' in actions)
E AssertionError: assert ('720308351' in [])
E + where '720308351' = str('720308351')
Test_AmqCase.py:33: AssertionError
---------------------------- Captured stdout call -----------------------------
Waiting for messages...
['OK', 'LOGIN=720308351', 'end', '']
Total time for event occur : 30.017589569091797
ACCOUNTS []