diff --git a/README.rst b/README.rst index 1c05f7e..131053a 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/pytest_slack/plugin.py b/pytest_slack/plugin.py index 1f13fce..f69f0bd 100644 --- a/pytest_slack/plugin.py +++ b/pytest_slack/plugin.py @@ -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): @@ -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: diff --git a/requirements_dev.txt b/requirements_dev.txt index 6d956cc..379bcbf 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -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 diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 625ac17..068b501 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -1,6 +1,8 @@ import json import mock +import pytest + def test_pytest_slack_failed(testdir): """Make sure that our pytest-slack works.""" @@ -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 = '' 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] @@ -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): @@ -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 = '' 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] @@ -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