Skip to content

Writing unit tests for an app using this SDK #1188

Description

@devangmehta123

I have seen mentions of unit tests in some issues like this one: #1052

But the documentation does not mention how we can write unit tests in our code if we integrate the slack sdk in our code base.

We use pytest along with moto3 to mock AWS APIs, for example. Is there something similar we can do for this slack sdk or do we have to make our own fixtures to make the unit tests work?

Activity

  1. added
    discussionM-T: An issue where more input is needed to reach a decision
    questionM-T: User needs support to use the project
    testsM-T: Testing work only
    and removed on Mar 3, 2022
  2. seratch commented on Mar 3, 2022

    @seratch
    Contributor

    Hi @devangmehta123, thanks for writing in!

    At this moment, there is no built-in support for testing your code using this SDK.

    As for the unit tests verifying the behavior of this SDK itself spins up a mock HTTP server with fixture data this way: https://github.com/slackapi/python-slack-sdk/tree/v3.15.2/tests/slack_sdk/web If you are fine with this approach, please feel free to reuse the mock server implementation for your tests.

    For the future, we are thinking of some better testing support for this low-level SDK and Bolt framework. The moto library is an amazing tool! I like it too. That being said, our forthcoming testing support for this SDK would be a bit simpler. Simulating the whole Slack platform is relatively complex. I cannot tell when the new module will be available yet but I myself already started early prototyping for it.

    I'm sorry for providing no clear answer at this moment. I hope this helps!

  3. devangmehta123 commented on Mar 8, 2022

    @devangmehta123
    Author

    Thanks @seratch , keep us all posted on the prototype.

    I am actually quite happy to use your mock HTTP server for the tests. If that could be made general purpose for unit tests, that could work.

    I have seen that approach used for database instances before and the only issue sometimes is the time it takes to start up and tear it down.

  4. github-actions commented on Apr 11, 2022

    @github-actions

    👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.

  5. added
    enhancementM-T: A feature request for new functionality
    and removed
    questionM-T: User needs support to use the project
    on Apr 11, 2022
  6. added this to the 3.x milestone on Apr 11, 2022
  7. changed the title [-]Writing unit tests against this SDK[/-] [+]Writing unit tests for an app using this SDK[/+] on Apr 11, 2022
  8. ryanandonian commented on Jun 5, 2023

    @ryanandonian

    Hey @devangmehta123 , I know this thread is over a year old, but I just wanted to chime in since I came here from Google looking for exactly the same piece of information. What I believe you were looking for was the same as me- a way to mock responses (similarly to how you can with responses or respx) but specifically for the slack_sdk API calls?

    The gist of it is I had to patch (using unittest.patch) urllib.request.urlopen calls, since the Slack SDK uses that under the hood. It's a little nasty, but this works out for me and it properly leverages the rest of the slack_sdk internals (like raising SlackApiError,SlackRequestError, retries, etc)

    @pytest.fixture
    def mock_slack_api_token_revoked_response():
        """Mocks a Slack API request to /api/users.profile.set and returns a "Token Revoked" payload
    
        Slack SDK uses urllib, so this is a bit hairy
        {'ok': False, 'error': 'token_revoked'}
        """
        with patch("slack_sdk.web.base_client.urlopen") as mock_urllib_open:
            mock_io_reader = MagicMock()
            mock_io_response = MagicMock()
            mock_io_reader.read.return_value = mock_io_response
            mock_io_response.decode.return_value = json.dumps({"ok": False, "error": "token_revoked"})
            mock_urllib_open.return_value = mock_io_reader
            yield mock_urllib_open

    I'm sure there's a better way to do this, but this works™ and has been really useful for testing certain error cases in my app which call the Slack API.

  9. gabrieljoelc commented on Mar 12, 2024

    @gabrieljoelc

    i ended up using httpretty:

    httpretty.register_uri(
        httpretty.POST,
        "https://www.slack.com/api/chat.postMessage",
        body=httpretty.Response(
            body=json.dumps(
                {
                    "ok": True,
                    "ts": "12345.678",
                }
            )
        ),
    )
  10. iamrajjoshi commented on Jun 28, 2024

    @iamrajjoshi

    Howdy! Us folks at Sentry.io are currently in the process of using the the Slack SDK for our Slack Bot. As part of the change, we would like to create a better testing suite. Ideally we would hit Slack's api or return mocked/ (preferably) cached responses from Slack. Are there any suggestions on how we can make this happen?

  11. ryanandonian commented on Jul 2, 2024

    @ryanandonian

    Howdy! Us folks at Sentry.io are currently in the process of using the the Slack SDK for our Slack Bot. As part of the change, we would like to create a better testing suite. Ideally we would hit Slack's api or return mocked/ (preferably) cached responses from Slack. Are there any suggestions on how we can make this happen?

    @iamrajjoshi If it's for non-End-to-End testing, could the solution that I or @gabrieljoelc posted help with that? That way you don't actually send http requests to Slack itself. My post patches the low level urllib (if you're reducing dependencies), while Gabriel's uses httpretty to patch it in a much more user-friendly way.

  12. ygalblum commented on Jul 15, 2024

    @ygalblum

    Hi, I also got here looking for a mock server.
    I found this: https://github.com/Skellington-Closet/slack-mock. But, IIUC this is JS specific.

    I started playing around with running a mock server as a prerequisite for the test and setting the base_url of the WebClient.
    The repo is https://github.com/ygalblum/slack-server-mock and I have a small test to show how this can be used: https://github.com/ygalblum/slack-server-mock/blob/main/test/test_echo.py.

    I'd love to get any feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Version: 3xdiscussionM-T: An issue where more input is needed to reach a decisionenhancementM-T: A feature request for new functionalitytestsM-T: Testing work only

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions