Home > Software engineering >  How can I just test the one line of code in an argument after mocking an argument
How can I just test the one line of code in an argument after mocking an argument

Time:08-21

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.

CodePudding user response:

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)
  • Related