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.