0
class PacketContext: capture_tstamp = None def __init__(self, capture_tstamp=None): self.capture_tstamp = capture_tstamp class SubParserMixin(): def __init__(self): self.context = PacketContext() def parser_er_data(packet): x = packet.payload def on_packet(self, packet): self.context.capture_tstamp = packet.capture_timestamp self.parse_er_data(packet.payload) #this mock test lives in another python file from exchanges.impl.tse.mixins import PacketContext def test_receive_timestamp(self): packet = MagicMock(capture_timestamp=123456) self.on_packet(packet) assert packet.context.capture_tstamp == 123456 

I have the above test that runs as I want it to, but there is one small issue- when self.on_packet(packet) is called with the mock packet, it of course will cause errors, in particular, due to the line

self.parse_er_data(packet.payload) 

Is there anyway to stop the code from running past the line

self.context.capture_tstamp = packet.capture_timestamp 

which is what we want to assert.

1 Answer 1

2

You could alter your

 def on_packet(self, packet): self.context.capture_tstamp = packet.capture_timestamp self.parse_er_data(packet.payload) 

to

 def on_packet(self, packet): self.context.capture_tstamp = packet.capture_timestamp return None # to be removed after debugging the line above self.parse_er_data(packet.payload) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Dalmas, but I will keeping in these unit tests forever. It is not a one- time test.
I actually mocked the parse_er_data() function for future reference, but the answer solves the problem, wehey

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.