Gubimand
Gubimand

Reputation: 11

Unit testing; How to mock out a class object which is going to be passed to a function in Python?

I'm in the means of doing a Python embedded project where I'm using Python-OpenZwave to communicate with a sensor via a z-wave dongle. And I need to do some unit testing on functions I have implemented. I'm not that experienced in unit testing, and have run into a problem with dependencies that I need to get rid of during the unit testing, because some of these funtions I have implemented communicates with the sensor and therefore only work on my embedded platform.

One of the functions I need to test is shown below, it takes 2 arguments, first is the ID of the sensor, the second is a network class object from the OpenZWave libary. It first sets the ID of the sensor we are working with in the class through a function call, second gets all data from the sensor and saves it as a dict in the class object, whereafter I search through all the data for the temperature value and return it;

def get_temperature(sensor_id, network_obj):
    multisensor = network_obj.nodes[sensor_id]
    multisensor.get_values()
    values = {}
    for value in multisensor.values:
        if multisensor.values[value].label == 'Temperature':
            values = {'Temperature': multisensor.values[value].data}
    return values

The thing I need help with, is how I can get rid of the dependency coming from the class object which is parsed to the function? I have tried methods I found online Patching, MonkeyPatching, Mock and MagicMock but haven't been able to make any of it work. So I would like some advice on how I can get rid of the dependencies and what libary / tool I should use in this case.

Thanks in advance

Upvotes: 1

Views: 83

Answers (0)

Related Questions