def test_main_function(self, mocked_square, mocked_cube): # underling function are mocks so calling main(5) will return mock mocked_square.return_value = 1 mocked_cube.return_value = 0 self.assertEquals(main(5), 1) mocked_square.assert_called_once_with(5) mocked_cube.assert_called_once_with(5)
def event_handle(event): print(event) try: userId = event['source']['userId'] except: print('error cannot get userId') return '' try: rtoken = event['replyToken'] except: print('error cannot get rtoken') return '' try: msgId = event["message"]["id"] msgType = event["message"]["type"] except: print('error cannot get msgID, and msgType') sk_id = np.random.randint(1, 17) replyObj = StickerSendMessage(package_id=str(1), sticker_id=str(sk_id)) line_bot_api.reply_message(rtoken, replyObj) return '' try: if msgType == "text": msg = str(event["message"]["text"]) function.main(msg) replyObj = TextSendMessage(text='เรียบร้อยครับ') line_bot_api.reply_message(rtoken, replyObj) else: sk_id = np.random.randint(1, 17) replyObj = StickerSendMessage(package_id=str(1), sticker_id=str(sk_id)) line_bot_api.reply_message(rtoken, replyObj) except: print('error cannot reply msg') replyObj_sk = StickerSendMessage(package_id=str(11537), sticker_id=str(52002755)) line_bot_api.reply_message(rtoken, replyObj_sk) replyObj_txt = TextSendMessage(text='ระบบขัดข้อง') line_bot_api.reply_message(rtoken, replyObj_txt) return ''
def test_output_contacts(): """Test function.""" # Construct a mock HTTP request. req = func.HttpRequest(method="GET", body=None, url="/api/HttpTrigger", params={"name": "Test"}) # Call the function. resp = function.main(req) # Check the output. assert resp.get_body().decode() == "Hello Test"
def test_main_function(monkeypatch): monkeypatch.setattr('function.square', lambda x: 1) monkeypatch.setattr('function.cube', lambda x: 0) assert main(5) == 1
# -*- coding: utf-8 -*- from Config import config from function import main num = 6 flag = config.True_Small # config.True_Small # True: 只测试小数据 main(num, flag=flag)