{"id":13771998,"url":"https://github.com/alm0ra/mockafka-py","last_synced_at":"2026-03-11T19:37:06.573Z","repository":{"id":203323681,"uuid":"709316651","full_name":"alm0ra/mockafka-py","owner":"alm0ra","description":"Mockafka-py is a Python library designed for in-memory mocking of Kafka.[aiokafka - confluence-kafka-python]","archived":false,"fork":false,"pushed_at":"2026-02-11T18:57:52.000Z","size":983,"stargazers_count":61,"open_issues_count":13,"forks_count":16,"subscribers_count":4,"default_branch":"main","last_synced_at":"2026-02-12T03:11:56.929Z","etag":null,"topics":["aiokafka","confluent-kafka-python","kafka","mock","mockafka"],"latest_commit_sha":null,"homepage":"https://mockafka-py.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alm0ra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"tidelift":"pypi/mockafka-py","github":"alm0ra"}},"created_at":"2023-10-24T13:27:12.000Z","updated_at":"2026-02-11T18:57:30.000Z","dependencies_parsed_at":"2023-11-12T21:26:44.349Z","dependency_job_id":"4dc07995-0870-4c74-8a1d-e671c2f9aacc","html_url":"https://github.com/alm0ra/mockafka-py","commit_stats":null,"previous_names":["xibalbas/mockafka-py","alm0ra/mockafka-py"],"tags_count":42,"template":false,"template_full_name":null,"purl":"pkg:github/alm0ra/mockafka-py","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alm0ra%2Fmockafka-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alm0ra%2Fmockafka-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alm0ra%2Fmockafka-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alm0ra%2Fmockafka-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alm0ra","download_url":"https://codeload.github.com/alm0ra/mockafka-py/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alm0ra%2Fmockafka-py/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30395921,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-11T18:46:22.935Z","status":"ssl_error","status_checked_at":"2026-03-11T18:46:17.045Z","response_time":84,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aiokafka","confluent-kafka-python","kafka","mock","mockafka"],"created_at":"2024-08-03T17:00:58.622Z","updated_at":"2026-03-11T19:37:06.544Z","avatar_url":"https://github.com/alm0ra.png","language":"Python","funding_links":["https://tidelift.com/funding/github/pypi/mockafka-py","https://github.com/sponsors/alm0ra"],"categories":["Mock and Stub"],"sub_categories":[],"readme":"![Alt text](banner.png)\n\u003cp align=\"center\"\u003e\n    \u003cem\u003eMockafka-py is a Python library designed for in-memory mocking of Kafka.\u003c/em\u003e\n\u003c/p\u003e\n\n![PyPI - Downloads](https://img.shields.io/pypi/dm/mockafka-py)\n![GitHub contributors](https://img.shields.io/github/contributors/alm0ra/mockafka-py)\n![Codecov](https://img.shields.io/codecov/c/github/alm0ra/mockafka-py)\n[![CodeFactor](https://www.codefactor.io/repository/github/alm0ra/mockafka-py/badge)](https://www.codefactor.io/repository/github/alm0ra/mockafka-py)\n[![codebeat badge](https://codebeat.co/badges/9cda14cf-3aac-438f-a6f7-c67517d7d74f)](https://codebeat.co/projects/github-com-alm0ra-mockafka-py-main)\n![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/alm0ra/mockafka-py/python-app.yml)\n![GitHub](https://img.shields.io/github/license/alm0ra/mockafka-py)\n![GitHub release (with filter)](https://img.shields.io/github/v/release/alm0ra/mockafka-py)\n![GitHub repo size](https://img.shields.io/github/repo-size/alm0ra/mockafka-py)\n\n# Mockafka: Fake Version for confluent-kafka-python \u0026 aiokafka\n\n# Features\n\n- Compatible with confluent-kafka\n- Compatible with aiokafka (async support)\n- Supports Produce, Consume, and AdminClient operations with ease.\n\n\n# Getting Start\n\n### Installing via pip or poetry\n\n```bash\npip install mockafka-py\n\n# or using poetry\npoetry add mockafka-py\n```\n\n\n# Usage\n\n## Multi-Decorator Examples for `confluent-kafka-python`\n\nIn the following examples, we showcase the usage of multiple decorators to simulate different scenarios in a Mockafka\nenvironment. These scenarios include producing, consuming, and setting up Kafka topics using the provided decorators.\n\n### Example 1: Using `@produce` and `@consume` Decorators\n\n#### Test Case: `test_produce_decorator`\n\n```python\nfrom mockafka import produce, consume\n\n\n@produce(topic='test', key='test_key', value='test_value', partition=4)\n@consume(topics=['test'])\ndef test_produce_and_consume_decorator(message):\n    \"\"\"\n    This test showcases the usage of both @produce and @consume decorators in a single test case.\n    It produces a message to the 'test' topic and then consumes it to perform further logic.\n    # Notice you may get message None\n    \"\"\"\n    # Your test logic for processing the consumed message here\n\n    if not message:\n        return\n\n    pass\n\n```\n\n### Example 2: Using Multiple `@produce` Decorators\n\n#### Test Case: `test_produce_twice`\n\n```python\nfrom mockafka import produce\n\n\n@produce(topic='test', key='test_key', value='test_value', partition=4)\n@produce(topic='test', key='test_key1', value='test_value1', partition=0)\ndef test_produce_twice():\n    # Your test logic here\n    pass\n```\n\n### Example 3: Using `@bulk_produce` and `@consume` Decorators\n\n#### Test Case: `test_bulk_produce_decorator`\n\n```python\nfrom mockafka import bulk_produce, consume\n\n\n@bulk_produce(list_of_messages=sample_for_bulk_produce)\n@consume(topics=['test'])\ndef test_bulk_produce_and_consume_decorator(message):\n    \"\"\"\n    This test showcases the usage of both @bulk_produce and @consume decorators in a single test case.\n    It does bulk produces messages to the 'test' topic and then consumes them to perform further logic.\n    \"\"\"\n    # Your test logic for processing the consumed message here\n    pass\n\n```\n\n### Example 4: Using `@setup_kafka` and `@produce` Decorators\n\n#### Test Case: `test_produce_with_kafka_setup_decorator`\n\n```python\nfrom mockafka import setup_kafka, produce\n\n\n@setup_kafka(topics=[{\"topic\": \"test_topic\", \"partition\": 16}])\n@produce(topic='test_topic', partition=5, key='test_', value='test_value1')\ndef test_produce_with_kafka_setup_decorator():\n    # Your test logic here\n    pass\n```\n\n### Example 5: Using `@setup_kafka`, Multiple `@produce`, and `@consume` Decorators\n\n#### Test Case: `test_consumer_decorator`\n\n```python\nfrom mockafka import setup_kafka, produce, consume\n\n\n@setup_kafka(topics=[{\"topic\": \"test_topic\", \"partition\": 16}])\n@produce(topic='test_topic', partition=5, key='test_', value='test_value1')\n@produce(topic='test_topic', partition=5, key='test_', value='test_value1')\n@consume(topics=['test_topic'])\ndef test_consumer_decorator(message: Message = None):\n    if message is None:\n        return\n    # Your test logic for processing the consumed message here\n    pass\n```\n\n## Using classes like confluent-kafka\n\n```python\nfrom mockafka import FakeProducer, FakeConsumer, FakeAdminClientImpl\nfrom mockafka.admin_client import NewTopic\nfrom random import randint\n\n# Create topic\nadmin = FakeAdminClientImpl()\nadmin.create_topics([\n    NewTopic(topic='test', num_partitions=5)\n])\n\n# Produce messages\nproducer = FakeProducer()\nfor i in range(0, 10):\n    producer.produce(\n        topic='test',\n        key=f'test_key{i}',\n        value=f'test_value{i}',\n        partition=randint(0, 4)\n    )\n\n# Subscribe consumer\nconsumer = FakeConsumer()\nconsumer.subscribe(topics=['test'])\n\n# Consume messages\nwhile True:\n    message = consumer.poll()\n    print(message)\n    consumer.commit()\n\n    if message is None:\n        break\n```\n\nOutput:\n\n```\n\"\"\"\n\u003cmockafka.message.Message object at 0x7fe84b4c3310\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c3370\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c33a0\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c33d0\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c3430\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c32e0\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c31f0\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c32b0\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c3400\u003e\n\u003cmockafka.message.Message object at 0x7fe84b4c3340\u003e\nNone\n\"\"\"\n```\n# Async support\n## Multi-Decorator Examples for `aiokafka`\n\n### Example 1: Using `@aproduce` and `@aconsume` and `@asetup_kafka` Decorators\n\n#### Test Case: `test_produce_and_consume_with_decorator`\n\n```python\nimport pytest\nfrom mockafka import aproduce, aconsume, asetup_kafka\n\n\n@pytest.mark.asyncio\n@asetup_kafka(topics=[{'topic': 'test_topic', 'partition': 16}], clean=True)\n@aproduce(topic='test_topic', value='test_value', key='test_key', partition=0)\n@aconsume(topics=['test_topic'])\nasync def test_produce_and_consume_with_decorator(message=None):\n    if not message:\n        return\n\n    assert message.key() == 'test_key'\n    assert message.value() == 'test_value'\n```\n\n### Example 2: Using `@aproduce` and `@asetup_kafka` Decorators\n\n#### Test Case: `test_produce_with_decorator`\n\n```python\nimport pytest\nfrom mockafka import aproduce, asetup_kafka\nfrom mockafka.aiokafka import FakeAIOKafkaConsumer\n\n@pytest.mark.asyncio\n@asetup_kafka(topics=[{'topic': 'test_topic', 'partition': 16}], clean=True)\n@aproduce(topic='test_topic', value='test_value', key='test_key', partition=0)\nasync def test_produce_with_decorator():\n    consumer = FakeAIOKafkaConsumer()\n    await consumer.start()\n    consumer.subscribe(['test_topic'])\n    message = await consumer.getone()\n\n    assert message.key() == 'test_key'\n    assert message.value() == 'test_value'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falm0ra%2Fmockafka-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falm0ra%2Fmockafka-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falm0ra%2Fmockafka-py/lists"}