Skip to content

Commit

Permalink
Merge pull request #19 from lithammer/configurable-icon-emoji
Browse files Browse the repository at this point in the history
Add flags to configure the icon emoji
  • Loading branch information
LaserPhaser committed May 8, 2019
2 parents a45ab16 + 9a3203e commit 6e5b080
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Usage
Set the reporter name
--slack_timeout=SLACK_TIMEOUT [DEFAULT = 10s ]
Set the timeout for sending results in seconds
--slack_success_icon=SLACK_SUCCESS_ICON [default = :thumbsup:]
Set icon for a successful run
--slack_failed_icon=SLACK_FAILED_ICON [default = :thumbsdown:]
Set icon for a failed run


Example
Expand Down
20 changes: 18 additions & 2 deletions pytest_slack/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ def pytest_addoption(parser):
help='Set the report send timeout'
)

group.addoption(
'--slack_success_icon',
action='store',
dest='slack_success_icon',
default=':thumbsup:',
help='Set icon for a successful run'
)

group.addoption(
'--slack_failed_icon',
action='store',
dest='slack_failed_icon',
default=':thumbsdown:',
help='Set icon for a failed run'
)


@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(terminalreporter, exitstatus, config):
Expand All @@ -66,10 +82,10 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):

if int(exitstatus) == 0:
color = "#56a64f"
emoji = ':thumbsup:'
emoji = config.option.slack_success_icon
else:
color = '#ff0000'
emoji = ':thumbsdown:'
emoji = config.option.slack_failed_icon

final_results = 'Passed=%s Failed=%s Skipped=%s Error=%s' % (passed, failed, skipped, error)
if report_link:
Expand Down
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ tox==3.9.0
coverage==4.5.3
Sphinx==2.0.1
twine==1.13.0
mock=2.0.0
mock==2.0.0
pytest==4.4.1
pytest-runner==4.4
42 changes: 38 additions & 4 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import mock

import pytest


def test_pytest_slack_failed(testdir):
"""Make sure that our pytest-slack works."""
Expand Down Expand Up @@ -30,12 +32,14 @@ def test_error(test):
slack_hook_username = 'regression Testing'
slack_hook_report_host = 'http://report_link.com'
slack_hook_channel = 'test'
slack_hook_icon_emoji = ':thumbsdown:'
expected_text = '<http://report_link.com|Passed=1 Failed=1 Skipped=1 Error=1>'
with mock.patch('requests.post') as mock_post:
testdir.runpytest('--slack_channel', slack_hook_channel,
'--slack_hook', slack_hook_host,
'--slack_report_link', slack_hook_report_host,
'--slack_username', slack_hook_username)
'--slack_username', slack_hook_username,
'--slack_failed_icon', slack_hook_icon_emoji)

called_data = json.loads(mock_post.call_args[1]['data'])
called_host = mock_post.call_args[0][0]
Expand All @@ -50,7 +54,7 @@ def test_error(test):
assert called_channel == slack_hook_channel
assert called_username == slack_hook_username
assert color == '#ff0000'
assert emoji == ':thumbsdown:'
assert emoji == slack_hook_icon_emoji


def test_pytest_slack_passed(testdir):
Expand All @@ -69,12 +73,14 @@ def test_pass():
slack_hook_username = 'regression Testing'
slack_hook_report_host = 'http://report_link.com'
slack_hook_channel = 'test'
slack_hook_icon_emoji = ':thumbsup:'
expected_text = '<http://report_link.com|Passed=1 Failed=0 Skipped=0 Error=0>'
with mock.patch('requests.post') as mock_post:
testdir.runpytest('--slack_channel', slack_hook_channel,
'--slack_hook', slack_hook_host,
'--slack_report_link', slack_hook_report_host,
'--slack_username', slack_hook_username)
'--slack_username', slack_hook_username,
'--slack_success_icon', slack_hook_icon_emoji)

called_data = json.loads(mock_post.call_args[1]['data'])
called_host = mock_post.call_args[0][0]
Expand All @@ -89,4 +95,32 @@ def test_pass():
assert called_channel == slack_hook_channel
assert called_username == slack_hook_username
assert color == '#56a64f'
assert emoji == ':thumbsup:'
assert emoji == slack_hook_icon_emoji


@pytest.mark.parametrize('test_input,expected_emoji', [
('1 == 1', ':sunny:'),
('2 == 1', ':rain_cloud:'),
])
def test_pytest_slack_custom_icons(testdir, test_input, expected_emoji):
testdir.makepyfile(
"""
def test_icon_emoji():
assert %s
""" % test_input
)

slack_hook_host = 'http://test.com/any_hash'
slack_hook_channel = 'test'
slack_hook_icon_emoji_success = ':sunny:'
slack_hook_icon_emoji_failed = ':rain_cloud:'
with mock.patch('requests.post') as mock_post:
testdir.runpytest('--slack_channel', slack_hook_channel,
'--slack_hook', slack_hook_host,
'--slack_success_icon', slack_hook_icon_emoji_success,
'--slack_failed_icon', slack_hook_icon_emoji_failed)

called_data = json.loads(mock_post.call_args[1]['data'])
emoji = called_data['icon_emoji']

assert emoji == expected_emoji

0 comments on commit 6e5b080

Please sign in to comment.