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

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, you 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 Distribution

slack_bolt_testcase-0.0.2.tar.gz (6.8 kB view details)

Uploaded Source

File details

Details for the file slack_bolt_testcase-0.0.2.tar.gz.

File metadata

  • Download URL: slack_bolt_testcase-0.0.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for slack_bolt_testcase-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6f1af1b296f7b792bb8a6446d4fb48359af09871340482b630294028fd42e1f6
MD5 fbc6eed4bdd1e43229611047730adad3
BLAKE2b-256 d6ef73d9f30c4d7ddd9861aa9ddf6d09a2a4cc5a298b6c736396320c0f046fc4

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