Best Python code snippet using yandex-tank
test_utils.py
Source:test_utils.py
1"""Unit-ÑеÑÑÑ ÑÑилиÑ"""2import sys3import os4import unittest5import json6sys.path.append(os.path.join(os.getcwd(), '..'))7from common.variables import RESPONSE, ERROR, USER, ACCOUNT_NAME, TIME, ACTION, PRESENCE, ENCODING8from common.utils import get_message, send_message, get_address_port9class TestSocket:10 '''11 ТеÑÑовÑй клаÑÑ Ð´Ð»Ñ ÑеÑÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð¾ÑпÑавки и полÑÑениÑ,12 пÑи Ñоздании ÑÑебÑÐµÑ ÑловаÑÑ, коÑоÑÑй бÑÐ´ÐµÑ Ð¿ÑогонÑÑÑÑ13 ÑеÑез ÑеÑÑовÑÑ ÑÑнкÑиÑ14 '''15 def __init__(self, test_dict):16 self.test_dict = test_dict17 self.encoded_message = None18 self.receved_message = None19 def send(self, message_to_send):20 """21 ТеÑÑÐ¾Ð²Ð°Ñ ÑÑнкÑÐ¸Ñ Ð¾ÑпÑавки, коÑÑеÑно кодиÑÑÐµÑ ÑообÑение,22 Ñак-же ÑоÑ
ÑанÑÐµÑ ÑÑо должно бÑло оÑпÑавлено в ÑокеÑ.23 message_to_send - Ñо, ÑÑо оÑпÑавлÑем в ÑокеÑ24 :param message_to_send:25 :return:26 """27 json_test_message = json.dumps(self.test_dict)28 # кодиÑÑÐµÑ ÑообÑение29 self.encoded_message = json_test_message.encode(ENCODING)30 # ÑоÑ
ÑанÑем ÑÑо должно бÑло оÑпÑавлено в ÑокеÑ31 self.receved_message = message_to_send32 def recv(self, max_len):33 """34 ÐолÑÑаем даннÑе из ÑокеÑа35 :param max_len:36 :return:37 """38 json_test_message = json.dumps(self.test_dict)39 return json_test_message.encode(ENCODING)40class Tests(unittest.TestCase):41 '''42 ТеÑÑовÑй клаÑÑ, ÑобÑÑвенно вÑполнÑÑÑий ÑеÑÑиÑование.43 '''44 test_dict_send = {45 ACTION: PRESENCE,46 TIME: 111111.111111,47 USER: {48 ACCOUNT_NAME: 'test_test'49 }50 }51 test_dict_recv_ok = {RESPONSE: 200}52 test_dict_recv_err = {53 RESPONSE: 400,54 ERROR: 'Bad Request'55 }56 def test_send_message(self):57 """58 ТеÑÑиÑÑем коÑÑекÑноÑÑÑ ÑабоÑÑ ÑÑкÑии оÑпÑавки,59 Ñоздадим ÑеÑÑовÑй ÑÐ¾ÐºÐµÑ Ð¸ пÑовеÑим коÑÑекÑноÑÑÑ Ð¾ÑпÑавки ÑловаÑÑ60 :return:61 """62 # ÑкземплÑÑ ÑеÑÑового ÑловаÑÑ, Ñ
ÑÐ°Ð½Ð¸Ñ ÑобÑÑвенно ÑеÑÑовÑй ÑловаÑÑ63 test_socket = TestSocket(self.test_dict_send)64 # вÑзов ÑеÑÑиÑÑемой ÑÑнкÑии, ÑезÑлÑÑаÑÑ Ð±ÑдÑÑ ÑоÑ
ÑÐ°Ð½ÐµÐ½Ñ Ð² ÑеÑÑовом ÑокеÑе65 send_message(test_socket, self.test_dict_send)66 # пÑовеÑка коÑÑеÑноÑÑи кодиÑÐ¾Ð²Ð°Ð½Ð¸Ñ ÑловаÑÑ.67 # ÑÑавниваем ÑезÑлÑÑÐ°Ñ Ð´Ð¾Ð²Ñенного кодиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð¸ ÑезÑлÑÑÐ°Ñ Ð¾Ñ ÑеÑÑиÑÑемой ÑÑнкÑии68 self.assertEqual(test_socket.encoded_message, test_socket.receved_message)69 # дополниÑелÑно, пÑовеÑим генеÑаÑÐ¸Ñ Ð¸ÑклÑÑениÑ, пÑи не ÑловаÑе на вÑ
оде.70 with self.assertRaises(Exception):71 send_message(test_socket, test_socket)72 def test_get_message(self):73 """74 ТеÑÑ ÑÑнкÑии пÑиÑма ÑообÑениÑ75 :return:76 """77 test_sock_ok = TestSocket(self.test_dict_recv_ok)78 test_sock_err = TestSocket(self.test_dict_recv_err)79 # ÑеÑÑ ÐºÐ¾ÑÑекÑной ÑаÑÑиÑÑовки коÑÑекÑного ÑловаÑÑ80 self.assertEqual(get_message(test_sock_ok), self.test_dict_recv_ok)81 # ÑеÑÑ ÐºÐ¾ÑÑекÑной ÑаÑÑиÑÑовки оÑибоÑного ÑловаÑÑ82 self.assertEqual(get_message(test_sock_err), self.test_dict_recv_err)83 84 def test_default_get_address_port(self):85 """ 86 ТеÑÑиÑÑÐµÑ ÑоÑмиÑование ipv4 адÑеÑа и поÑÑа по ÑмолÑаниÑ87 """88 ipv4_port_default = [ '', 7777]89 90 test_ipv4_port = get_address_port()91 self.assertEqual(test_ipv4_port,ipv4_port_default)92 93 94 95if __name__ == '__main__':...
test_flask.py
Source:test_flask.py
...12 headers = {'Host': 'my-host-0'}13 result = extract_connection_params(headers)14 assert result['hostname'] == 'my-host-0'15 assert 'port' not in result16 def test_ipv4_port(self):17 headers = {'Host': '192.168.1.1:9497'}18 result = extract_connection_params(headers)19 assert result['hostname'] == '192.168.1.1'20 assert result['port'] == 949721 def test_ipv4_no_port(self):22 headers = {'Host': '192.168.1.1'}23 result = extract_connection_params(headers)24 assert result['hostname'] == '192.168.1.1'25 assert 'port' not in result26 def test_ipv6_port(self):27 headers = {'Host': '[::1]:9497'}28 result = extract_connection_params(headers)29 assert result['hostname'] == '::1'30 assert result['port'] == 9497...
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!!