Skip to main content

Test case for your Slack bots

Project description

slack-bolt-testcase

test License Maintenance made-with-python PyPI pyversions PyPI version slack-bolt-testcase PyPI status PyPI download month Test Coverage

A simple unittest TestCase class to use it in your Slack Bolt App unittest tests

Introduction

The Slack bolt library and the Slack SDK for Python are the best and official ways of implementing Slack bots in Python. However, there is a small piece missing there: no official documentation about how to do tests with the standard unittest library that allow developers to test that they have registered their handlers correctly. slack-bolt-testcase fixes that.

Requirements

  • Python >= 3.9
  • slack_bolt~=1.23.0
  • slack_sdk~=3.35.0

Use

To use this TestCase, ypython setup.pyou just need to inherit from EventTestCase and make sure you have registered your event handlers in the AppTestCase.app.

from unittest.mock import call

from slack_bolt import Say

from slack_bolt_testcase.event_test_case import EventTestCase

# Inherit from the EventTestCase
class TestEventExample(EventTestCase):
    def test_handled_event(self):
        # Make sure you have registered your events in a way with the self.app mocked Slack App object
        # you can do it here or pass the self.app object to a register function.
        @self.app.event('app_mention')
        def handle_message_events(event: dict[str, str], say: Say):
            # Ensure we are receiving the right text
            if event.get('text') == 'hello':
                event_type = event.get('type')
                say(f'Hello the handler handled a(n) {event_type}!')

        # Trigger the event
        self.trigger_event(event_type='app_mention', text='hello', user='U12345')

        # Check the assertions
        self.assertEqual(
            [call(f'Hello the handler handled a(n) app_mention!')],
            self.mock_say.call_args_list,
        )

More examples in the tests folder.

License

MIT

Disclaimer

Slack is a trademark of Slack Technologies, LLC. This project is not affiliated with, endorsed by, or sponsored by Slack Technologies, LLC. The use of the name "Slack" in this repository is solely for descriptive purposes and does not imply any association or intent to infringe on any trademarks.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slack_bolt_testcase-0.0.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file slack_bolt_testcase-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for slack_bolt_testcase-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 72d1162f31558dada4637468a4330fac72d88aca17deee74db6f2fd522e7e8d7
MD5 e5b68e2605ba70d647510acda0dba232
BLAKE2b-256 ed822b2b91e896ab2ff0d698da73c26e592ce42a193ccddb4ea5bb701f7e934e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page