简体   繁体   English

如何模拟 Python 单元测试的 Kafka Producer 和 producer.send 方法

[英]How to mock Kafka Producer and producer.send method for Python unit test

I am new to Python and trying to write a unit test which involves Kafka.我是 Python 的新手,并试图编写一个涉及 Kafka 的单元测试。 I have a class with a function which calls another function to initialize Kafka Producer and then call producer.send().我有一个 class 和一个 function,它调用另一个 function 来初始化 Kafka Producer,然后调用 producer.send()。 I want to mock Kafka Producer for my unit test.我想为我的单元测试模拟 Kafka Producer。

Below is my code and I want to write unit test for produce_kafka_message method.下面是我的代码,我想为produce_kafka_message 方法编写单元测试。

class KafkaProducerIntface
    
    def __init__(self, topic, .....)
        self.bootstrap_server = bootstrap_server
        self.producer = None
        self.post_topic = topic
    ....

    def produce_kafka_message(self, key, value, headers)

        self.__initialize_producer__(retries=3)
        future = self.producer.send(self.topic, key=key, value=value, headers=headers)
        self.producer.flush()


    def __initialize_producer__(self, retries=3)
        self.producer = KafkaProducer(bootstrap_servers=self.bootstrap_server, acks='all', retries=retries)

You can use mock lib for it and mock the produce_kafka_message method:您可以使用 mock lib 并模拟produce_kafka_message方法:

with mock.patch.object(KafkaProducerIntface, "produce_kafka_message") as mock_produce_kafka_message:
    ... # do some code here
    mock_produce_kafka_message.assert_called_once_with(key, value, headerss)

You can also do the same with KafkaProducer class it self:您也可以对 KafkaProducer class 执行相同的操作:

with mock.patch("kafka.path.KafkaProducer") as mock_kafka_producer:
     interface = KafkaProducerIntface(...)
     interface.produce_kafka_message(key, value, headers)
     
     mock_kafka_producer.send.assert_called_once()
     mock_kafka_producer.flush()

BTW.顺便提一句。 also please check if you want to run flush after the send or just.poll(0):)还请检查是否要在发送后运行刷新或 just.poll(0):)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kafka-python producer.send 未在 try-except 块中收到,但确实与 time.sleep(1) 一起发送 - Kafka-python producer.send isn't being recieved in a try-except block, but does send with time.sleep(1) Kafka-python Producer执行Send,但Kafka没有数据到达 - Kafka-python Producer performs Send, but no data arrives in Kafka 如何运行用Python编写的kafka生成器? - How to run kafka producer that is written in Python? Kafka 生产者 JSON 值与 Python - Kafka producer JSON value with Python 如何使用多个引导服务器创建 python kafka 生产者? - How to create python kafka producer with multiple bootstrap servers? 当某些代理不可用时,python kafka生产者如何工作? - How does a python kafka producer work when some of brokers are not available? 如何使用python在kafka中创建多个生产者和消费者 - How to create multiple producer and consumer in kafka using python 我想使用带有 python beautifulsoup 的 kafka 生产者向 kafka 经纪人发送消息 - I want to use kafka producer with python beautifulsoup to send message to kafka broker RabbitMQ:如何在 Python 生产者和消费者之间发送 Python 字典? - RabbitMQ: How to send Python dictionary between Python producer and consumer? 带有Python生产者的Jhipster / Spring Kafka消费者 - Jhipster/Spring Kafka Consumer with a Python Producer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM