0

example.py

def simple():
   msg = consumer.poll(timeout=int(timeout))
   if msg is None:
     break
  if msg.error():
    if (msg.error().code() == KafkaError.UNKNOWN_TOPIC_OR_PART):
              response_code = 409
              self.logger.debug("Error reading message : {}".format(msg.error()))
              break

when i mock (consumer.poll) it showing error, TypeError: can't set attributes of built_in/extension type 'cimpl.Consumer'

@mock.patch('confluent_kafka.Consumer.poll')
def test_simple(mock_poll):
    mock_poll.return_value
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

As the error message says, you can't patch the C extension class. As a remedy, you can derive the class like this.(It shows the new style syntax for a fixture. Using an annotation is deprecated.)

import confluent_kafka import Consumer as _Consumer

class Consumer(_Consumer): pass

def get_cls_full_name(cls):
    return cls.__module__ + '.' + cls.__name__

def test_consumer(mocker):
    mock_poll = mocker.patch(get_cls_full_name(Consumer) + '.poll')
    ...
relent95
  • 3,703
  • 1
  • 14
  • 17