python测试confluent_kafka消费者

时间:2018-08-09 09:01:54

标签: python python-3.x pytest python-unittest confluent-kafka

我正在尝试使用pytest测试以下代码:

self.consumer = Consumer({
                'group.id': config['KAFKA_GROUP_ID'],
                'bootstrap.servers': config['KAFKA_BROKERS'],
                'default.topic.config': {"auto.offset.reset": "smallest"}
    }
)
self.consumer.subscribe([config['KAFKA_TOPIC']])
while True:
    collect = self.consumer.poll()
    if collect is None:
        continue

    try:
        message = json.loads(collect.value().decode('utf-8'))
    except json.JSONDecodeError:
        continue

    return message

但是我不能模拟订阅功能。我尝试过:

mock_subscribe = MagicMock(return_value='test')
monkeypatch.setattr('confluent_kafka.cimpl.Consumer.subscribe', mock_subscribe)

结果是出现以下错误:

TypeError: can't set attributes of built-in/extension type

如何正确模拟此功能?

0 个答案:

没有答案