How to mock Kafka Producer and producer.send method in PyTests

61 Views Asked by At

I am using Python Django Project and I have Kafka implemented in my project. Now I am writing my pytest cases for APIs and functions. However, I am unable to mock the Kafka Producers and Consumers.

I have three main classes, functions, and APIs.

  1. Written pytest
  2. API
  3. KafkaProducer class

This is my pytest - test_api.py

import pytest

def test_my_api(api_client, headers):
    url = "call_my_api/2"
    json_data = "request_data"
    res = api_client.put(url, data=json_data, **headers)
    
    assert res.status_code == 200

This is my KafkaProducer class - producer.py

from kafka import KafkaProducer
from eventHandler.singletop import Singleton

import json
import os

class Producer(metaclass=Singleton):
    conn = None
    admin_client = None

    def __init__(self):
        if self.conn is None:
            self.conn = KafkaProducer(bootstrap_servers=os.getenv('KAFKA_BROKERS').split(','), api_version="1",
                                      value_serializer=lambda x: json.dumps(x).encode('utf-8'))

    def sendMessage(self, topic, message):
        self.conn.send(topic, message)

This is my API. - views.py

from producer import Producer

class MyAPIView(APIView):
    def put(self, request):
        try:
            data = request.data
            kafka_producer = Producer()
            kafka_producer.sendMessage("kafka_producer_topic", data)
            return True
        except Exception:
            return False

I need to mock Kafka producer with KAFKA_BROKER = localhost:9092.
Kafka does not try to make the connection to the broker. And usage of sendMessage does not throw any error.
If we can create a mocking fixture of this class.
I have tried many solutions but I am not able to do it. Please help thanks in advance.

0

There are 0 best solutions below