Open th0ger opened 3 weeks ago
Same here with:
You can work arround it by using the FakeConsumer
and FakeProducer
classes.
In my case, I needed to pass a Kafka Message to the function I'm testing, so I added this function in my tests file:
import pytest
import json
from confluent_kafka import Message
from mockafka import FakeConsumer, FakeProducer, setup_kafka
def get_fake_kafka_message(payload: dict) -> Message:
consumer = FakeConsumer()
producer = FakeProducer()
consumer.subscribe(['test.topic'])
producer.produce(
key='test_key',
value=json.dumps(payload),
topic='test.topic',
partition=0
)
message = consumer.poll()
return message
Then, in my test:
@setup_kafka(topics=[{"topic": "test.topic", "partition": 1}])
def test_run_creation_message():
my_payload = {...}
message = get_fake_kafka_message(my_payload)
# Rest of my test
It's a bit annoying to have to jump through so many hoops, but at least we can work arround it while waiting for a fix to this bug :/
is it possible to use a FakeProducer and a real Consumer in a test with this approach?
@asetup_kafka(topics=[{'topic': 'test_topic', 'partition': 16}], clean=True)
@aproduce(topic='test_topic', value='test_value', key='test_key', partition=0)
async def test_produce_with_decorator(self):
consumer = AIOKafkaConsumer('test_topic', bootstrap_servers='localhost:9092')
await consumer.start()
consumer.subscribe(['test_topic'])
message = await consumer.getone()
assert message.key == b'test_key'
assert message.value == b'test_value'
This doesn't seem to work as
> raise KafkaConnectionError(f"No connection to node with id {node_id}")
E aiokafka.errors.KafkaConnectionError: KafkaConnectionError: No connection to node with id 1
Describe the bug Following the basic use cases in the README does not work. pytest returns
fixture 'message' not found
. Tested with poetry and pip.To Reproduce
Expected behavior Test passes successfully.
Desktop (please complete the following information):