Skip to content

Commit

Permalink
time taken + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Ryabkov committed Dec 10, 2020
1 parent 6e029ef commit 8a576c8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pytest_telegram/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import datetime
import time

import pytest
import requests

Expand Down Expand Up @@ -110,6 +113,9 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
final_results = 'Passed=%s Failed=%s Skipped=%s Error=%s XFailed=%s XPassed=%s' % (
passed, failed, skipped, error, xfailed, xpassed)

session_time = time.time() - terminalreporter._sessionstarttime
time_taken = f'\nTime taken: {str(time.strftime("%H:%M:%S", time.gmtime(session_time)))}'

if failed == 0 and error == 0:
sticker_payload = {'chat_id': chat_id, 'sticker': success_sticker_id}
else:
Expand All @@ -119,6 +125,6 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
if not disable_stickers:
message_id = requests.post(f'{telegram_uri}/sendSticker', json=sticker_payload).json()['result']['message_id']
message_payload = {'chat_id': chat_id,
'text': f'{final_results}{custom_text}{report_url}\n{failed_tests}{error_tests}',
'text': f'{final_results}{time_taken}{custom_text}{report_url}\n{failed_tests}{error_tests}',
'reply_to_message_id': message_id}
requests.post(f'{telegram_uri}/sendMessage', json=message_payload).json()
46 changes: 46 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def test_xpass():
telegram_report_url = 'http://report_link.com'
fail_sticker_id = 'CAACAgIAAxkBAAMIX8rohSxoNbodB1D38VZx9HI2CDwAAmIBAAIQGm0izcITZBkXtbceBA'
expected_text = 'Passed=1 Failed=1 Skipped=1 Error=1 XFailed=1 XPassed=1' \
'\nTime taken: 00:00:00' \
'\nhttp://report_link.com\n'
with mock.patch('requests.post') as mock_post:
testdir.runpytest('--telegram_id', telegram_chat_id,
Expand Down Expand Up @@ -77,6 +78,7 @@ def test_pass():
telegram_report_url = 'http://report_link.com'
success_sticker_id = 'CAACAgIAAxkBAAMHX8roD4u8f7DCsRobma1dZNuCeBwAAlkBAAIQGm0iHZVsOV_OQB8eBA'
expected_text = 'Passed=1 Failed=0 Skipped=0 Error=0 XFailed=0 XPassed=0' \
'\nTime taken: 00:00:00' \
'\nhttp://report_link.com\n'
with mock.patch('requests.post') as mock_post:
testdir.runpytest('--telegram_id', telegram_chat_id,
Expand Down Expand Up @@ -114,6 +116,7 @@ def test_fail():
telegram_report_url = 'http://report_link.com'
fail_sticker_id = 'CAACAgIAAxkBAAMIX8rohSxoNbodB1D38VZx9HI2CDwAAmIBAAIQGm0izcITZBkXtbceBA'
expected_text = 'Passed=0 Failed=1 Skipped=0 Error=0 XFailed=0 XPassed=0' \
'\nTime taken: 00:00:00' \
'\nhttp://report_link.com\n' \
'\nFailed tests:' \
'\ntest_list_failed_telegram.py::test_fail\n'
Expand All @@ -137,6 +140,7 @@ def test_fail():
assert message_chat_id == telegram_chat_id
assert send_sticker == fail_sticker_id


def test_list_failed_with_dots_telegram(testdir):
"""Make sure plugin sends limited amount of failed tests."""

Expand All @@ -155,6 +159,7 @@ def test_fail(id):
telegram_report_url = 'http://report_link.com'
fail_sticker_id = 'CAACAgIAAxkBAAMIX8rohSxoNbodB1D38VZx9HI2CDwAAmIBAAIQGm0izcITZBkXtbceBA'
expected_text = 'Passed=0 Failed=10 Skipped=0 Error=0 XFailed=0 XPassed=0' \
'\nTime taken: 00:00:00' \
'\nhttp://report_link.com\n' \
'\nFailed tests:' \
'\ntest_list_failed_with_dots_telegram.py::test_fail[0]' \
Expand All @@ -175,6 +180,47 @@ def test_fail(id):
message_text = message_request[2]['json']['text']
message_chat_id = message_request[2]['json']['chat_id']

assert sticker_called_url == f'https://api.telegram.org/bot{telegram_token}/sendSticker'
assert message_called_url == f'https://api.telegram.org/bot{telegram_token}/sendMessage'
assert message_text == expected_text
assert message_chat_id == telegram_chat_id
assert send_sticker == fail_sticker_id


def test_time_taken(testdir):
"""Make sure plugin sends limited amount of failed tests."""

testdir.makepyfile(
"""
import pytest
import time
def test_fail():
time.sleep(1)
assert 1 != 1
"""
)

telegram_token = 'Token'
telegram_chat_id = '130559633'
telegram_report_url = 'http://report_link.com'
fail_sticker_id = 'CAACAgIAAxkBAAMIX8rohSxoNbodB1D38VZx9HI2CDwAAmIBAAIQGm0izcITZBkXtbceBA'
expected_text = 'Passed=0 Failed=1 Skipped=0 Error=0 XFailed=0 XPassed=0' \
'\nTime taken: 00:00:01' \
'\nhttp://report_link.com\n'
with mock.patch('requests.post') as mock_post:
testdir.runpytest('--telegram_id', telegram_chat_id,
'--telegram_token', telegram_token,
'--telegram_report_url', telegram_report_url)

sticker_request = mock_post.mock_calls[0]
sticker_called_url = sticker_request[1][0]
send_sticker = sticker_request[2]['json']['sticker']
message_request = mock_post.mock_calls[4]
message_called_url = message_request[1][0]
message_text = message_request[2]['json']['text']
message_chat_id = message_request[2]['json']['chat_id']

assert sticker_called_url == f'https://api.telegram.org/bot{telegram_token}/sendSticker'
assert message_called_url == f'https://api.telegram.org/bot{telegram_token}/sendMessage'
assert message_text == expected_text
Expand Down

0 comments on commit 8a576c8

Please sign in to comment.