-
Notifications
You must be signed in to change notification settings - Fork 837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing unit tests for an app using this SDK #1188
Comments
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 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 The gist of it is I had to patch (using @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",
}
)
),
) |
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. |
Hi, I also got here looking for a mock server. I started playing around with running a mock server as a prerequisite for the test and setting the base_url of the WebClient. I'd love to get any feedback. |
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?
The text was updated successfully, but these errors were encountered: