0

Recently I've been trying to learn unit testing and now I'm on the concept mocking. I'm writing my code with python and using pytest library to test my code. I've a simple factory class has only one function that creates a KafkaConsumer, and I want to test that function. This is the factory class:

from kafka import KafkaConsumer
import json

class KafkaFactory:
    
    @staticmethod
    def consumer(topic, group_id, server):
        return KafkaConsumer(
            topic,
            group_id=group_id,
            bootstrap_servers=server,
            auto_offset_reset='earliest',
            enable_auto_commit=False,
            value_deserializer=lambda v: json.loads(v.decode('utf-8'))
        )

This is my unit test:

from src.factories.kafka import KafkaFactory

class TestKafkaFactory:
    
    def test_consumer(self, mocker):
        mocker.patch('kafka.KafkaConsumer').return_value = 'consumer'
        spy_factory = mocker.spy(KafkaFactory, 'consumer')
        KafkaFactory.consumer('fake-topic', 'fake-group-id', 'fake-server')
        
        assert spy_factory.spy_return == 'consumer'

But when I run that test, I see that KafkaConsumer tries to connect a kafka client and I get NoBrokerAvailable expection. I've assumed that when I use mocker.patch method, it does not try to find a client and gives me a mock object. What am I missing? Thank you.

  • 2
    You are patching the wrong object, see [where to patch](https://docs.python.org/3/library/unittest.mock.html#id6). As you are using `from kafka import KafkaConsumer`, you have to patch `KafkaConsumer` in your factory module, e.g. something like `mocker.patch('kafka_factory.KafkaConsumer')`, if `kafka_factory` is the name of that module. – MrBean Bremen Feb 20 '22 at 14:12
  • You're a wonderful person, thank you so much :) – Oğuz Öztınaz Feb 20 '22 at 20:25

0 Answers0