Skip to content

Commit ee202c9

Browse files
committed
Added option only_failed
Closes #231
1 parent fec2e78 commit ee202c9

6 files changed

Lines changed: 107 additions & 22 deletions

File tree

docs/conf.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#
2121
import os
2222
import sys
23+
2324
sys.path.insert(0, os.path.abspath('..'))
2425

2526
import pytest_messenger
@@ -78,7 +79,6 @@
7879
# If true, `todo` and `todoList` produce output, else they produce nothing.
7980
todo_include_todos = False
8081

81-
8282
# -- Options for HTML output -------------------------------------------
8383

8484
# The theme to use for HTML and HTML Help pages. See the documentation for
@@ -97,13 +97,11 @@
9797
# so a file named "default.css" will overwrite the builtin "default.css".
9898
html_static_path = ['_static']
9999

100-
101100
# -- Options for HTMLHelp output ---------------------------------------
102101

103102
# Output file base name for HTML help builder.
104103
htmlhelp_basename = 'pytest_messengerdoc'
105104

106-
107105
# -- Options for LaTeX output ------------------------------------------
108106

109107
latex_elements = {
@@ -133,7 +131,6 @@
133131
u'LaserPhaser', 'manual'),
134132
]
135133

136-
137134
# -- Options for manual page output ------------------------------------
138135

139136
# One entry per manual page. List of tuples
@@ -144,7 +141,6 @@
144141
[author], 1)
145142
]
146143

147-
148144
# -- Options for Texinfo output ----------------------------------------
149145

150146
# Grouping the document tree into Texinfo files. List of tuples
@@ -158,6 +154,3 @@
158154
'One line description of project.',
159155
'Miscellaneous'),
160156
]
161-
162-
163-

pytest_messenger/dingtalk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import hmac
44
import time
55
import urllib.parse
6-
import json
6+
77
import requests
88

99

pytest_messenger/slack.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import requests
21
import json
32

3+
import requests
4+
45

56
def add_slack_options(parser):
67
group = parser.getgroup('pytest-messenger[slack]')
@@ -90,10 +91,19 @@ def add_slack_options(parser):
9091
help='Set icon (a url) for a failed run. Overrides slack_failed_icon'
9192
)
9293

94+
group.addoption(
95+
'--only_failed',
96+
action='store',
97+
dest='only_failed',
98+
default=False,
99+
help='Send only failed results to the messenger'
100+
)
101+
93102

94103
def slack_send_message(test_result, config, exitstatus):
95104
timeout = config.option.slack_timeout
96105
report_link = config.option.slack_report_link
106+
only_failed = config.option.only_failed
97107
slack_hook = config.option.slack_hook
98108
channel = config.option.slack_channel
99109
ssl_verify = config.option.ssl_verify
@@ -107,13 +117,22 @@ def slack_send_message(test_result, config, exitstatus):
107117
color = '#ff0000'
108118
emoji = config.option.slack_failed_emoji
109119
icon = config.option.slack_failed_icon
110-
final_results = 'Passed=%s Failed=%s Skipped=%s Error=%s XFailed=%s XPassed=%s' % (
111-
test_result.passed,
112-
test_result.failed,
113-
test_result.skipped,
114-
test_result.error,
115-
test_result.xfailed,
116-
test_result.xpassed)
120+
if only_failed:
121+
if test_result.failed == 0 and test_result.error == 0:
122+
return # Do not send anything when all passed and no errors.
123+
final_results = 'Failed=%s Error=%s' % (
124+
test_result.failed,
125+
test_result.error,
126+
)
127+
128+
else:
129+
final_results = 'Passed=%s Failed=%s Skipped=%s Error=%s XFailed=%s XPassed=%s' % (
130+
test_result.passed,
131+
test_result.failed,
132+
test_result.skipped,
133+
test_result.error,
134+
test_result.xfailed,
135+
test_result.xpassed)
117136
if report_link:
118137
final_results = '<%s|%s>' % (report_link, final_results)
119138
if message_prefix:

requirements_pub.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ tox==3.19.0
77
coverage==5.2
88
Sphinx==3.3.0
99
twine==3.1.1
10-
pytest==6.2.1
10+
pytest
1111
mock==4.0.1
1212
pytest-runner==5.2

tests/test_ding.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import json
21
import mock
32

4-
import pytest
5-
63

74
def test_pytest_messenger_ding_failed(testdir):
85
"""Make sure that our pytest-messenger works."""

tests/test_slack.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
2-
import mock
32

3+
import mock
44
import pytest
55

66

@@ -234,3 +234,79 @@ def test_icon_url():
234234

235235
assert url == expected_url
236236
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

Comments
 (0)