unit test for webhook
See original GitHub issueHi @gonchik
I noticed you have #806 merged. Thank you.
As we just invoke the original rest api #806, the unit tests would be too simple.
Do we need to add this kind of code style of unit test for this feature? If yes, I’d like to summit another PR. If no, could you please show some better code style of unit test?
Here is the snippet. It seems stupid, but it does work.
from atlassian import Bitbucket
from unittest.mock import patch
from unittest import TestCase
class TestWebhook(TestCase):
def setUp(self):
self.fake_data = {
"project_key": "fake_project",
"repository_slug": "fake_repo",
"name": "fake_name",
"events": ["repo:refs_changed", "pr:merged", "pr:opened"],
"webhook_url": "https://example.com",
"active": True,
"secret": "fake_secret",
}
def test_create_webhook(self):
return_value = {
"id": 10,
"name": "fake_name",
"createdDate": 1513106011000,
"updatedDate": 1513106011000,
"events": [
"repo:refs_changed",
"pr:merged",
"pr:opened"
],
"configuration": {
"secret": "fake_secret"
},
"url": "https://example.com",
"active": True
}
with patch.object(Bitbucket, "post", return_value=return_value):
bitbucket = Bitbucket(url="https://bitbucket.example.com", username="username", password="password")
rst = bitbucket.create_webhook(self.fake_data["project_key"], self.fake_data["repository_slug"],
self.fake_data["name"], self.fake_data["events"],
self.fake_data["webhook_url"], self.fake_data["active"])
self.assertEqual(rst["name"], self.fake_data["name"])
self.assertEqual(rst["events"], self.fake_data["events"])
self.assertEqual(rst["configuration"]["secret"], self.fake_data["secret"])
self.assertEqual(rst["url"], self.fake_data["webhook_url"])
self.assertEqual(rst["active"], self.fake_data["active"])
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
How to test webhooks and response statuses using MailSlurp
For automated testing we can use MailSlurp @mailslurp/test-webhooks library to setup real http endpoints that can respond with a set of responses we...
Read more >Testing Webhook Receivers | Svix Resoures
Testing webhook receivers is similar to testing an API endpoint, though the difference here ... Unit tests are used to test the smallest...
Read more >How to create a GitHub Action to run Unit Tests & perform ...
First, we use actions/checkout, this will check out the code and allow our Action to run the NPM commands successfully. We then provide...
Read more >How to write Unit test cases for Web hooks in c#? - Stack ...
I want to mock the web hook request (Post request) in unit test cases and pass the request headers and body as the...
Read more >Testing Webhooks > Stripe Level 2 - SymfonyCasts
The first strategy - and the one we use here on KnpU - is to create an automated test that sends a fake...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @Spacetown
Thanks for your advice.
I re-considered this approach of test and found it is really amazing.
I narrowly considered the test is unit test, but it’s more like an integration test here.
Session.request
is mocked in the furthest edge parts we cannot control. That’s a good point.The rest tests will be added these days.
Besides, this inspires me to add tests to Artifactory Project.
@zhan9san That was the reason why I introduced this mookup. Ther where a couble of errors in refactroing the BB implementation. For implementing the OO interface I searched for a way to test all functions in the interface and implemnted this mookup. It’s a little bit of work to implement the responses but than you can test a specific endpoint in detail.