Best Python code snippet using lemoncheesecake
test_report_slack.py
Source:test_report_slack.py
...10 backend = SlackReportingBackend()11 with env_vars(**vars):12 return backend.create_reporting_session(None, None, None, None)13@pytest.fixture14def slacker_mock(mocker):15 mocker.patch("slacker.Slacker")16 return mocker17@lcc.suite("suite")18class suite_sample:19 @lcc.test("test")20 def test(self):21 lcc.log_info("do stuff")22try:23 import slacker24except ImportError:25 pass # slacker is not installed (slack is an optional feature), skip tests26else:27 def test_create_reporting_session_success(tmpdir):28 _test_reporting_session(LCC_SLACK_AUTH_TOKEN="sometoken", LCC_SLACK_CHANNEL="#chan")...
test_logging_slack.py
Source:test_logging_slack.py
1import logging2import sys3from logging import LogRecord4import pytest5from mock import patch, MagicMock6from pnp.logging import SlackHandler7@patch('pnp.logging.slacker.Slacker')8def test_logger_emit_for_smoke(slacker_mock):9 slacker_mock.return_value = MagicMock()10 dut = SlackHandler(11 api_key='doesnt_matter',12 channel='pytest',13 fire_and_forget=False14 )15 dut.emit(LogRecord(16 name='pytest',17 level=logging.ERROR,18 pathname='doesnt_matter',19 lineno=42,20 msg='LogRecord from pytest',21 args=None,22 exc_info=None23 ))24 slacker_mock.return_value.chat.post_message.assert_called_once_with(25 text=None,26 channel='#pytest',27 username=SlackHandler.DEFAULT_USERNAME,28 icon_url=None,29 icon_emoji=SlackHandler.DEFAULT_EMOJI,30 attachments=[{31 'color': 'danger',32 'fields': [{'title': 'LogRecord from pytest', 'short': False}]33 }]34 )35@patch('pnp.logging.slacker.Slacker')36def test_logger_emit_with_trace(slacker_mock):37 slacker_mock.return_value = MagicMock()38 dut = SlackHandler(39 api_key='doesnt_matter',40 channel='pytest',41 fire_and_forget=False42 )43 dut.MAX_ATTACHMENT_CHARS = 4644 try:45 raise Exception("EXCEPTION RAISED ON PURPOSE!")46 except Exception:47 dut.emit(LogRecord(48 name='pytest',49 level=logging.ERROR,50 pathname='doesnt_matter',51 lineno=42,52 msg='LogRecord from pytest',53 args=None,54 exc_info=sys.exc_info()55 ))56 slacker_mock.return_value.chat.post_message.assert_called_once_with(57 text=None,58 channel='#pytest',59 username=SlackHandler.DEFAULT_USERNAME,60 icon_url=None,61 icon_emoji=SlackHandler.DEFAULT_EMOJI,62 attachments=[{63 'color': 'danger',64 'fields': [{65 'title': 'LogRecord from pytest',66 'short': False,67 'value': '```Exception: EXCEPTION RAISED ON PURPOSE!\n```'68 }]69 }]70 )71@patch('pnp.logging.slacker.Slacker')72def test_ping_users_existing_user(slacker_mock):73 # user_list = self.slacker.users.list().body['members']74 slacker_mock.return_value = MagicMock()75 slacker_mock.return_value.users.list.return_value.body = {76 'members': [{77 'id': 42,78 'name': 'pytest_user',79 'profile': {80 'real_name': 'PyTest',81 'display_name': 'PyTest'82 }83 }]84 }85 dut = SlackHandler(86 api_key='doesnt_matter',87 channel='pytest',88 ping_users=['pytest_user'],89 fire_and_forget=False90 )91 assert dut.ping_user_ids == [42]92 dut.emit(LogRecord(93 name='pytest',94 level=logging.ERROR,95 pathname='doesnt_matter',96 lineno=42,97 msg='LogRecord from pytest',98 args=None,99 exc_info=None100 ))101 slacker_mock.return_value.chat.post_message.assert_called_once_with(102 text="<@42> ",103 channel='#pytest',104 username=SlackHandler.DEFAULT_USERNAME,105 icon_url=None,106 icon_emoji=SlackHandler.DEFAULT_EMOJI,107 attachments=[{108 'color': 'danger',109 'fields': [{'title': 'LogRecord from pytest', 'short': False}]110 }]111 )112@patch('pnp.logging.slacker.Slacker')113def test_ping_users_non_existing_user(slacker_mock):114 # user_list = self.slacker.users.list().body['members']115 slacker_mock.return_value = MagicMock()116 slacker_mock.return_value.users.list.return_value.body = {117 'members': [{118 'id': 42,119 'name': 'pytest_user',120 'profile': {121 'real_name': 'PyTest',122 'display_name': 'PyTest'123 }124 }]125 }126 with pytest.raises(RuntimeError) as exc:127 SlackHandler(128 api_key='doesnt_matter',129 channel='pytest',130 ping_users=['doesnotexist'],131 fire_and_forget=False132 )...
test_notify_slack.py
Source:test_notify_slack.py
1import pytest2from mock import MagicMock3from pnp.plugins.push.notify import Slack4@pytest.mark.asyncio5async def test_slack_push(mocker):6 slacker_mock = mocker.patch('pnp.plugins.push.notify.slacker.Slacker')7 slacker_mock.return_value = MagicMock()8 dut = Slack(9 api_key='doesnt_matter',10 channel='pytest',11 name='pytest'12 )13 await dut.push('Simple message')14 slacker_mock.return_value.chat.post_message.assert_called_once_with(15 text='Simple message',16 channel='#pytest',17 username=Slack.DEFAULT_USERNAME,18 icon_emoji=Slack.DEFAULT_EMOJI19 )20@pytest.mark.asyncio21async def test_slack_push_with_ping_user(mocker):22 slacker_mock = mocker.patch('pnp.plugins.push.notify.slacker.Slacker')23 slacker_mock.return_value = MagicMock()24 slacker_mock.return_value.users.list.return_value.body = {25 'members': [{26 'id': 42,27 'name': 'pytest_user',28 'profile': {29 'real_name': 'PyTest',30 'display_name': 'PyTest'31 }32 }, {33 'id': 99,34 'name': 'another_user',35 'profile': {36 'real_name': 'anon',37 'display_name': 'anusr'38 }39 }]40 }41 dut = Slack(42 api_key='doesnt_matter',43 channel='pytest',44 ping_users=['PyTest'],45 name='pytest'46 )47 # Use the real_name48 await dut.push('Simple message')49 slacker_mock.return_value.chat.post_message.assert_called_with(50 text='<@42> Simple message',51 channel='#pytest',52 username=Slack.DEFAULT_USERNAME,53 icon_emoji=Slack.DEFAULT_EMOJI54 )55 # Envelope override with name56 await dut.push({'data': 'Simple message', 'ping_users': 'another_user'})57 slacker_mock.return_value.chat.post_message.assert_called_with(58 text='<@99> Simple message',59 channel='#pytest',60 username=Slack.DEFAULT_USERNAME,61 icon_emoji=Slack.DEFAULT_EMOJI62 )63 # Envelope override with unknown user64 await dut.push({'data': 'Simple message', 'ping_users': 'unknown_user'})65 slacker_mock.return_value.chat.post_message.assert_called_with(66 text='Simple message',67 channel='#pytest',68 username=Slack.DEFAULT_USERNAME,69 icon_emoji=Slack.DEFAULT_EMOJI...
test_slack.py
Source:test_slack.py
1from unittest import TestCase2from unittest.mock import patch3from reconbot.notifiers.slack import SlackNotifier4@patch('reconbot.notifiers.slack.Slacker')5class SlackNotifierTest(TestCase):6 def setUp(self):7 self.username = 'myname'8 self.channel = '#mychannel'9 def test_notify_sends_chat_message(self, slacker_mock):10 notifier = SlackNotifier('myapikeyhere', self.username, self.channel)11 notifier.notify('some text')12 notifier.slack.chat.post_message.assert_called_with(13 self.channel,14 "some text",15 parse='none',16 username=self.username17 )18 def test_notify_all_mentions_channel(self, slacker_mock):19 notifier = SlackNotifier('myapikeyhere', self.username, self.channel, 'all')20 notifier.notify('some text')21 notifier.slack.chat.post_message.assert_called_with(22 self.channel,23 "<!channel>:\nsome text",24 parse='none',25 username=self.username26 )27 def test_notify_online_mentions_here(self, slacker_mock):28 notifier = SlackNotifier('myapikeyhere', self.username, self.channel, 'online')29 notifier.notify('some text')30 notifier.slack.chat.post_message.assert_called_with(31 self.channel,32 "<!here>:\nsome text",33 parse='none',34 username=self.username35 )36 def test_notify_to_custom_channel(self, slacker_mock):37 new_channel = '#myotherchannel'38 notifier = SlackNotifier('myapikeyhere', self.username, self.channel, 'online')39 notifier.notify('some text', options={ 'channel': new_channel })40 notifier.slack.chat.post_message.assert_called_with(41 new_channel,42 "<!here>:\nsome text",43 parse='none',44 username=self.username...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!