Best Python code snippet using tavern
test_mqtt_response.py
Source:test_mqtt_response.py
...21 def __init__(self, returned):22 self.topic = returned["topic"]23 self.payload = returned["payload"].encode("utf8")24class TestResponse(object):25 def _get_fake_verifier(self, expected, fake_messages):26 """Given a list of messages, return a mocked version of the MQTT27 response verifier which will take messages off the front of this list as28 if they were published29 This mocks it as if all messages were returned in order, which they30 might not have been...?31 """32 if not isinstance(fake_messages, list):33 pytest.fail("Need to pass a list of messages")34 def yield_all_messages():35 msg_copy = fake_messages[:]36 def inner(timeout):37 try:38 return msg_copy.pop(0)39 except IndexError:40 return None41 return inner42 fake_client = Mock(43 spec=MQTTClient,44 message_received=yield_all_messages(),45 )46 return MQTTResponse(fake_client, "Test stage", expected, {})47 def test_message_on_same_topic_fails(self):48 """Correct topic, wrong message"""49 expected = {50 "topic": "/a/b/c",51 "payload": "hello",52 }53 fake_message = FakeMessage({54 "topic": "/a/b/c",55 "payload": "goodbye",56 })57 verifier = self._get_fake_verifier(expected, [fake_message])58 with pytest.raises(exceptions.TestFailError):59 verifier.verify(expected)60 assert len(verifier.received_messages) == 161 assert verifier.received_messages[0].topic == fake_message.topic62 def test_correct_message(self):63 """Both correct matches"""64 expected = {65 "topic": "/a/b/c",66 "payload": "hello",67 }68 fake_message = FakeMessage(expected)69 verifier = self._get_fake_verifier(expected, [fake_message])70 verifier.verify(expected)71 assert len(verifier.received_messages) == 172 assert verifier.received_messages[0].topic == fake_message.topic73 def test_correct_message_eventually(self):74 """One wrong messge, then the correct one"""75 expected = {76 "topic": "/a/b/c",77 "payload": "hello",78 }79 fake_message_good = FakeMessage(expected)80 fake_message_bad = FakeMessage({81 "topic": "/a/b/c",82 "payload": "goodbye",83 })84 verifier = self._get_fake_verifier(expected, [fake_message_bad, fake_message_good])85 verifier.verify(expected)86 assert len(verifier.received_messages) == 287 assert verifier.received_messages[0].topic == fake_message_bad.topic...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!