|
1 | 1 | import json |
2 | | -import mock |
3 | 2 |
|
| 3 | +import mock |
4 | 4 | import pytest |
5 | 5 |
|
6 | 6 |
|
@@ -234,3 +234,79 @@ def test_icon_url(): |
234 | 234 |
|
235 | 235 | assert url == expected_url |
236 | 236 | assert emoji is None |
| 237 | + |
| 238 | + |
| 239 | +def test_only_failed(testdir): |
| 240 | + """Make sure that our pytest-messenger works.""" |
| 241 | + |
| 242 | + testdir.makepyfile( |
| 243 | + """ |
| 244 | + import pytest |
| 245 | + def test_pass(): |
| 246 | + assert 1 == 1 |
| 247 | +
|
| 248 | +
|
| 249 | + def test_fail(): |
| 250 | + assert 1 == 2 |
| 251 | +
|
| 252 | + def test_error(test): |
| 253 | + assert 1 == "" |
| 254 | + """ |
| 255 | + ) |
| 256 | + |
| 257 | + slack_hook_host = 'http://test.com/any_hash' |
| 258 | + slack_hook_username = 'regression Testing' |
| 259 | + slack_hook_report_host = 'http://report_link.com' |
| 260 | + slack_hook_channel = 'test' |
| 261 | + slack_hook_icon_emoji = ':thumbsdown:' |
| 262 | + expected_text = '<http://report_link.com|Failed=1 Error=1>' |
| 263 | + with mock.patch('requests.post') as mock_post: |
| 264 | + testdir.runpytest('--slack_channel', slack_hook_channel, |
| 265 | + '--slack_hook', slack_hook_host, |
| 266 | + '--slack_report_link', slack_hook_report_host, |
| 267 | + '--slack_username', slack_hook_username, |
| 268 | + '--slack_failed_emoji', slack_hook_icon_emoji, |
| 269 | + '--only_failed', True) |
| 270 | + |
| 271 | + called_data = json.loads(mock_post.call_args[1]['data']) |
| 272 | + called_host = mock_post.call_args[0][0] |
| 273 | + called_channel = called_data['channel'] |
| 274 | + called_username = called_data['username'] |
| 275 | + text = called_data['attachments'][0]['text'] |
| 276 | + color = called_data['attachments'][0]['color'] |
| 277 | + emoji = called_data['icon_emoji'] |
| 278 | + |
| 279 | + assert called_host == slack_hook_host |
| 280 | + assert text == expected_text |
| 281 | + assert called_channel == slack_hook_channel |
| 282 | + assert called_username == slack_hook_username |
| 283 | + assert color == '#ff0000' |
| 284 | + assert emoji == slack_hook_icon_emoji |
| 285 | + |
| 286 | + |
| 287 | +def test_only_failed_no_fails(testdir): |
| 288 | + """Make sure that our pytest-messenger works.""" |
| 289 | + |
| 290 | + testdir.makepyfile( |
| 291 | + """ |
| 292 | + import pytest |
| 293 | + def test_pass(): |
| 294 | + assert 1 == 1 |
| 295 | +
|
| 296 | + """ |
| 297 | + ) |
| 298 | + |
| 299 | + slack_hook_host = 'http://test.com/any_hash' |
| 300 | + slack_hook_username = 'regression Testing' |
| 301 | + slack_hook_report_host = 'http://report_link.com' |
| 302 | + slack_hook_channel = 'test' |
| 303 | + slack_hook_icon_emoji = ':thumbsdown:' |
| 304 | + |
| 305 | + with mock.patch('requests.post') as mock_post: |
| 306 | + testdir.runpytest('--slack_channel', slack_hook_channel, |
| 307 | + '--slack_hook', slack_hook_host, |
| 308 | + '--slack_report_link', slack_hook_report_host, |
| 309 | + '--slack_username', slack_hook_username, |
| 310 | + '--slack_failed_emoji', slack_hook_icon_emoji, |
| 311 | + '--only_failed', True) |
| 312 | + assert mock_post.call_args is None |
0 commit comments