Writing unit tests for an app using this SDK
I have seen mentions of unit tests in some issues like this one: https://github.com/slackapi/python-slack-sdk/issues/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?
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!
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.
👋 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.
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.
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",
}
)
),
)