-
Notifications
You must be signed in to change notification settings - Fork 245
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
Document about the ways to write unit tests for Bolt apps #380
Comments
Hi @offbyone, thanks for taking the time to write in! As we have been receiving feedback about testing, it's on our radar. I cannot tell when we can release built-in test support but I myself will be working on it in the latter half of this year. At this moment, the only thing I can suggest is to do something similar with the tests this project has. You can find so many tests under this package: https://github.com/slackapi/bolt-python/tree/v1.6.1/tests/scenario_tests The key points are:
In addition to above,
You can use any testing frameworks (my recommendation is pytest). The forthcoming testing support won't require your tests to rely on a specific test framework. We may provde some additional utility for pytest etc. for convenience, though.
For I know that this approach is not so easy but it should be reasonable enough. Please consider going with it for now. I hope this was helpful to you. If everything is clear for now, would you mind closing this issue? For the testing support task, I will come up with a bit more detailed issue soon. Also, for better documentation, we will add the section for testing once we release built-in testing support. |
Yeah, it ... helps. I guess I'd consider, if I were you, leaving this issue open to track that there is a gap, and resolving it when that documentation exists. If you don't track issues that way, please feel free to close it, but as far as I'm concerned this is still a bug until I can go to your documentation site and see instructions on how to test my bot :D |
Yeah, I understand this way. It's also fine to keep this open for now. I have a link to this in the new issue that organizes the tasks/discussions around testing support. Thanks for the nice suggestion. |
I have a further question: Since this kind of testing requires constructing the |
Hey @seratch is there any update on when this will be released? |
@carlos-alberto I did a quick prototyping late last year. After that, we haven't had good progress due to other priorities. But we're aiming to provide a better solution sometime this year! |
If it helps those who stumble on this in the future, this is an open-source Bolt app with a moderately sized test suite as an example to dig through. We've taken the approach of keeping our core functionality in a separate module that doesn't depend on Then in app.py you can just import the module and use the Slack @decorated functions essentially as wrappers over your unit-testable business logic. ⚠ This only helps for unit tests, unfortunately haven't delved into integration tests, so unfortunately no review/feedback on seratch's suggestion. |
Any progress on this since the last update? |
Hey @jimenezj8, this is still on my radar and actually I did quick prototyping last year. However, I cannot tell when we can deliver a stable solution due to our bandwidth and priorities. We will update you when we come up with something in the future. |
Thanks for the update @seratch 🙂 will keep my eyes peeled for another |
Hi i was looking into how to write unit tests for our slack bot and found this wonderful library. Just be aware that the 1.1 version is broken if you want to use it use 1.0.5. |
Came across this as well and figured I'd try to contribute a bit. Here's some modified code from my current app, hopefully it can help someone else. Normally I'd have all of the payload stuff in my conftest.py file to keep things clean, but for the purposes of this I figured I'd put it all in one place. import pytest
from unittest.mock import AsyncMock
from my.production.code import easter_egg
# This is the shortcut we expect to receive from Slack (well, a heavily truncated one)
mock_shortcut = {
"message": {
"text": "Hello, World!",
"ts": "1234567890.123456"
},
"user": {
"id": "U123456",
"name": "John Doe"
},
"channel": {
"name": "general"
},
"trigger_id": "trigger_12345"
}
# Upon receiving this shortcut, we will send a 'view' to our user
# This is what we expect it to look like
expected_modal = {
"type": "modal",
"callback_id": "hello-modal",
"title": {
"type": "plain_text",
"text": "Greetings!"
},
"submit": {
"type": "plain_text",
"text": "Good Bye"
},
"blocks":[
{
"type": "section",
"text": {
"type": "plain_text",
"text": "Frosty the Snowman",
"emoji": True
}
},
{
"type": "video",
"title": {
"type": "plain_text",
"text": "Frosty the Snowman",
"emoji": True
},
"title_url": "https://www.youtube.com/watch?v=RRxQQxiM7AA",
"description": {
"type": "plain_text",
"text": "Frosty the Snowman",
"emoji": True
},
"video_url": "https://www.youtube.com/embed/vjscH2WBWjw?feature=oembed&autoplay=1",
"alt_text": "Frosty the Snowman",
"thumbnail_url": "https://i.ytimg.com/vi/bSzBBK8gC6c/hqdefault.jpg",
"author_name": "",
"provider_name": "YouTube",
"provider_icon_url": "https://a.slack-edge.com/80588/img/unfurl_icons/youtube.png"
}
]
}
@pytest.mark.asyncio
async def test_easter_egg():
ack = AsyncMock()
client = AsyncMock()
client.views_open = AsyncMock()
# Test my actual production function 'easter_egg'
# This should ack the shortcut and then open a view to the user
await easter_egg(ack, mock_shortcut, client)
ack.assert_awaited_once # Make sure it was acknowledged
client.views_open.assert_awaited_once_with( # Make sure the view we get back is correct
trigger_id=mock_shortcut["trigger_id"],
view=expected_modal
) |
Any progress on this since the last update? |
@leon-adarcewicz the The tests for Workflow Buddy from @I-Dont-Remember are also solid at testing the overall listener logic while mocking HTTP requests is another great option from @fr12k 🙏 I'm not aware of updates to built-in test helpers for Bolt Python, but these all seem like good testing approaches to me. I hope this is helpful for now, and in some neartime I'll explore this a bit more to include on slack.dev/bolt-python 📚 |
Thank you @zimeg! |
@seratch Any feedback on the current state of unit-testing support? |
No updates from me lately. To align with our platform roadmap and priorities, I am not actively working on the prototype right now. |
This may help with testing, but I haven't tried it yet: |
I am looking to write unit tests for my Bolt API, which consists of an app with several handlers for events / messages. The handlers are both decorated using the
app.event
decorator, and make use of theapp
object to access things like thedb
connection that has been put on it. For example:The thing is, I cannot find any framework pointers, or documentation, on how I would write reasonable unit tests for this. Presumably ones where the kwargs like
ack
andrespond
are replaced by test doubles. How do I do this?The page URLs
Requirements
Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.
The text was updated successfully, but these errors were encountered: