Best Python code snippet using lisa_python
test_websocket.py
Source:test_websocket.py  
1# -*- coding:utf-8 -*-2import ddt3import unittest4import urllib5import urllib.request6import json7import os8import sys, getopt9sys.path.append('..')10from utils.config import Config11from utils.taskdata import TaskData, Task12from utils.logger import LoggerInstance13from utils.parametrizedtestcase import ParametrizedTestCase14from utils.api.websocketapi import WebSocketApi15from utils.api.commonapi import *16from utils.base import WebSocket17time.sleep(2)18print("stop all")19stop_all_nodes()20print("start all")21start_nodes([0,1,2,3,4,5,6], Config.DEFAULT_NODE_ARGS, True, True)22time.sleep(60)23print("waiting for 60s......")24from ws_api_conf import Conf25logger = LoggerInstance26wsapi = WebSocketApi()27class TestWebAPI(ParametrizedTestCase):28		29	def start(self, log_path):30		logger.open(log_path)31	def normal_finish(self, task_name, log_path, result, msg):32		if result:33			logger.print("[ OK       ] ")34			logger.append_record(task_name, "pass", log_path)35		else:36			logger.print("[ Failed   ] " + msg)37			logger.append_record(task_name, "fail", log_path)38		logger.close()39	def abnormal_finish(self, task_name, log_path, result, msg):40		if not result:41			logger.print("[ OK       ] ")42			logger.append_record(task_name, "pass", log_path)43		else:44			logger.print("[ Failed   ] " + msg)45			logger.append_record(task_name, "fail", log_path)46		logger.close()47	48	def test_01_heartbeat(self):49		log_path = "01_heartbeat.log"50		task_name = "01_heartbeat"51		self.start(log_path)52		(result, response) = wsapi.heartbeat()53		self.normal_finish(task_name, log_path, result, "")54	def test_02_heartbeat(self):55		log_path = "02_heartbeat.log"56		task_name = "02_heartbeat"57		self.start(log_path)58		stop_node(0)59		(result, response) = wsapi.heartbeat()60		start_node(0, Config.DEFAULT_NODE_ARGS)61		time.sleep(5)62		self.abnormal_finish(task_name, log_path, result, "")63	64	def test_993_heartbeat(self):65		log_path = "03_heartbeat.log"66		task_name = "03_heartbeat"67		self.start(log_path)68		try:69			ws = WebSocket()70			ws.exec(heartbeat_gap=400)71			# (result, response) = wsapi.heartbeat()72		except:73			pass74		self.normal_finish(task_name, log_path, True, "")75	def test_04_subscribe(self):76		log_path = "04_subscribe.log"77		task_name = "04_subscribe"78		self.start(log_path)79		time.sleep(5)80		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT])81		self.normal_finish(task_name, log_path, result, "")82	def test_05_subscribe(self):83		log_path = "05_subscribe.log"84		task_name = "05_subscribe"85		self.start(log_path)86		time.sleep(5)87		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_INCORRECT_2])88		self.normal_finish(task_name, log_path, result, "")89	90	def test_06_subscribe(self):91		log_path = "06_subscribe.log"92		task_name = "06_subscribe"93		self.start(log_path)94		time.sleep(5)95		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_INCORRECT_3])96		self.normal_finish(task_name, log_path, result, "")97	def test_07_subscribe(self):98		log_path = "07_subscribe.log"99		task_name = "07_subscribe"100		self.start(log_path)101		time.sleep(5)102		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sevent=True)103		self.normal_finish(task_name, log_path, result, "")104	def test_08_subscribe(self):105		log_path = "08_subscribe.log"106		task_name = "08_subscribe"107		self.start(log_path)108		time.sleep(5)109		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sevent=False)110		self.normal_finish(task_name, log_path, result, "")111	def test_09_subscribe(self):112		log_path = "09_subscribe.log"113		task_name = "09_subscribe"114		self.start(log_path)115		time.sleep(5)116		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sevent=None)117		self.normal_finish(task_name, log_path, result, "")118	def test_10_subscribe(self):119		log_path = "10_subscribe.log"120		task_name = "10_subscribe"121		self.start(log_path)122		(result, response) = wsapi.subscribe(None, sevent=True)123		self.normal_finish(task_name, log_path, result, "")124	def test_11_subscribe(self):125		log_path = "11_subscribe.log"126		task_name = "11_subscribe"127		self.start(log_path)128		(result, response) = wsapi.subscribe(None, sevent=0)129		self.normal_finish(task_name, log_path, result, "")130	def test_12_subscribe(self):131		log_path = "12_subscribe.log"132		task_name = "12_subscribe"133		self.start(log_path)134		time.sleep(5)135		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sjsonblock=True)136		self.normal_finish(task_name, log_path, result, "")137	def test_13_subscribe(self):138		log_path = "13_subscribe.log"139		task_name = "13_subscribe"140		self.start(log_path)141		time.sleep(5)142		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sjsonblock=False)143		self.normal_finish(task_name, log_path, result, "")144	def test_14_subscribe(self):145		log_path = "14_subscribe.log"146		task_name = "14_subscribe"147		self.start(log_path)148		time.sleep(5)149		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sjsonblock=None)150		self.normal_finish(task_name, log_path, result, "")151	def test_15_subscribe(self):152		log_path = "15_subscribe.log"153		task_name = "15_subscribe"154		self.start(log_path)155		time.sleep(5)156		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sjsonblock=0)157		self.normal_finish(task_name, log_path, result, "")158	def test_16_subscribe(self):159		log_path = "16_subscribe.log"160		task_name = "16_subscribe"161		self.start(log_path)162		time.sleep(5)163		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], srawblock=True)164		self.normal_finish(task_name, log_path, result, "")165	def test_17_subscribe(self):166		log_path = "17_subscribe.log"167		task_name = "17_subscribe"168		self.start(log_path)169		time.sleep(5)170		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], srawblock=False)171		self.normal_finish(task_name, log_path, result, "")172	def test_18_subscribe(self):173		log_path = "18_subscribe.log"174		task_name = "18_subscribe"175		self.start(log_path)176		time.sleep(5)177		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], srawblock=None)178		self.normal_finish(task_name, log_path, result, "")179	def test_19_subscribe(self):180		log_path = "19_subscribe.log"181		task_name = "19_subscribe"182		self.start(log_path)183		time.sleep(5)184		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], srawblock=0)185		self.normal_finish(task_name, log_path, result, "")186	def test_20_subscribe(self):187		log_path = "20_subscribe.log"188		task_name = "20_subscribe"189		self.start(log_path)190		time.sleep(5)191		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sblocktxhashs=True)192		self.normal_finish(task_name, log_path, result, "")193	def test_21_subscribe(self):194		log_path = "21_subscribe.log"195		task_name = "21_subscribe"196		self.start(log_path)197		time.sleep(5)198		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sblocktxhashs=False)199		self.normal_finish(task_name, log_path, result, "")200	def test_22_subscribe(self):201		log_path = "22_subscribe.log"202		task_name = "22_subscribe"203		self.start(log_path)204		time.sleep(5)205		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sblocktxhashs=None)206		self.normal_finish(task_name, log_path, result, "")207	def test_23_subscribe(self):208		log_path = "23_subscribe.log"209		task_name = "23_subscribe"210		self.start(log_path)211		time.sleep(5)212		(result, response) = wsapi.subscribe([Conf.CONTRACT_ADDRESS_CORRECT], sblocktxhashs=0)213		self.normal_finish(task_name, log_path, result, "")214	def test_24_getgenerateblocktime(self):215		log_path = "24_getgenerateblocktime.log"216		task_name = "24_getgenerateblocktime"217		self.start(log_path)218		(result, response) = wsapi.getgenerateblocktime()219		self.normal_finish(task_name, log_path, result, "")220	def test_25_getgenerateblocktime(self):221		log_path = "25_getgenerateblocktime.log"222		task_name = "25_getgenerateblocktime"223		self.start(log_path)224		(result, response) = wsapi.getgenerateblocktime({"height":"1"})225		self.normal_finish(task_name, log_path, result, "")226	def test_26_getconnectioncount(self):227		log_path = "26_getconnectioncount.log"228		task_name = "26_getconnectioncount"229		self.start(log_path)230		(result, response) = wsapi.getconnectioncount()231		self.normal_finish(task_name, log_path, result, "")232	def test_27_getconnectioncount(self):233		log_path = "27_getconnectioncount.log"234		task_name = "27_getconnectioncount"235		self.start(log_path)236		stop_node(0)237		(result, response) = wsapi.getconnectioncount()238		start_node(0, Config.DEFAULT_NODE_ARGS)239		time.sleep(5)240		self.abnormal_finish(task_name, log_path, result, "")241	def test_28_getconnectioncount(self):242		log_path = "28_getconnectioncount.log"243		task_name = "28_getconnectioncount"244		self.start(log_path)245		(result, response) = wsapi.getconnectioncount({"height":"1"})246		self.normal_finish(task_name, log_path, result, "")247	def test_29_getblocktxsbyheight(self):248		log_path = "29_getblocktxsbyheight.log"249		task_name = "29_getblocktxsbyheight"250		self.start(log_path)251		(result, response) = wsapi.getblocktxsbyheight(Conf.HEIGHT_CORRECT)252		self.normal_finish(task_name, log_path, result, "")253	def test_31_getblocktxsbyheight(self):254		log_path = "31_getblocktxsbyheight.log"255		task_name = "31_getblocktxsbyheight"256		self.start(log_path)257		(result, response) = wsapi.getblocktxsbyheight(Conf.HEIGHT_BORDER)258		self.normal_finish(task_name, log_path, result, "")259	def test_32_getblocktxsbyheight(self):260		log_path = "32_getblocktxsbyheight.log"261		task_name = "32_getblocktxsbyheight"262		self.start(log_path)263		(result, response) = wsapi.getblocktxsbyheight(Conf.HEIGHT_INCORRECT_1)264		self.abnormal_finish(task_name, log_path, result, "")265	def test_33_getblocktxsbyheight(self):266		log_path = "33_getblocktxsbyheight.log"267		task_name = "33_getblocktxsbyheight"268		self.start(log_path)269		(result, response) = wsapi.getblocktxsbyheight(Conf.HEIGHT_INCORRECT_2)270		self.abnormal_finish(task_name, log_path, result, "")271	def test_34_getblocktxsbyheight(self):272		log_path = "34_getblocktxsbyheight.log"273		task_name = "34_getblocktxsbyheight"274		self.start(log_path)275		(result, response) = wsapi.getblocktxsbyheight(Conf.HEIGHT_INCORRECT_3)276		self.abnormal_finish(task_name, log_path, result, "")277	def test_35_getblockbyheight(self):278		log_path = "35_getblockbyheight.log"279		task_name = "35_getblockbyheight"280		self.start(log_path)281		(result, response) = wsapi.getblockbyheight(Conf.HEIGHT_CORRECT)282		self.normal_finish(task_name, log_path, result, "")283	def test_37_getblockbyheight(self):284		log_path = "37_getblockbyheight.log"285		task_name = "37_getblockbyheight"286		self.start(log_path)287		(result, response) = wsapi.getblockbyheight(Conf.HEIGHT_BORDER)288		self.normal_finish(task_name, log_path, result, "")289	def test_38_getblockbyheight(self):290		log_path = "38_getblockbyheight.log"291		task_name = "38_getblockbyheight"292		self.start(log_path)293		(result, response) = wsapi.getblockbyheight(Conf.HEIGHT_INCORRECT_1)294		self.abnormal_finish(task_name, log_path, result, "")295	def test_39_getblockbyheight(self):296		log_path = "39_getblockbyheight.log"297		task_name = "39_getblockbyheight"298		self.start(log_path)299		(result, response) = wsapi.getblockbyheight(Conf.HEIGHT_INCORRECT_2)300		self.abnormal_finish(task_name, log_path, result, "")301	def test_40_getblockbyheight(self):302		log_path = "40_getblockbyheight.log"303		task_name = "40_getblockbyheight"304		self.start(log_path)305		(result, response) = wsapi.getblockbyheight(Conf.HEIGHT_INCORRECT_3)306		self.abnormal_finish(task_name, log_path, result, "")307	def test_41_getblockbyhash(self):308		log_path = "41_getblockbyhash.log"309		task_name = "41_getblockbyhash"310		self.start(log_path)311		(result, response) = wsapi.getblockbyhash(Conf.BLOCK_HASH_CORRECT)312		self.normal_finish(task_name, log_path, result, "")313	314	def test_43_getblockbyhash(self):315		log_path = "43_getblockbyhash.log"316		task_name = "43_getblockbyhash"317		self.start(log_path)318		(result, response) = wsapi.getblockbyhash(Conf.BLOCK_HASH_INCORRECT_1)319		self.abnormal_finish(task_name, log_path, result, "")320	def test_44_getblockbyhash(self):321		log_path = "44_getblockbyhash.log"322		task_name = "44_getblockbyhash"323		self.start(log_path)324		(result, response) = wsapi.getblockbyhash(Conf.BLOCK_HASH_INCORRECT_2)325		self.abnormal_finish(task_name, log_path, result, "")326	def test_45_getblockbyhash(self):327		log_path = "45_getblockbyhash.log"328		task_name = "45_getblockbyhash"329		self.start(log_path)330		(result, response) = wsapi.getblockbyhash(Conf.BLOCK_HASH_INCORRECT_3)331		self.abnormal_finish(task_name, log_path, result, "")332	def test_46_getblockbyhash(self):333		log_path = "46_getblockbyhash.log"334		task_name = "46_getblockbyhash"335		self.start(log_path)336		(result, response) = wsapi.getblockbyhash(Conf.BLOCK_HASH_INCORRECT_4)337		self.abnormal_finish(task_name, log_path, result, "")338	def test_47_getblockheight(self):339		log_path = "47_getblockheight.log"340		task_name = "47_getblockheight"341		self.start(log_path)342		(result, response) = wsapi.getblockheight()343		self.normal_finish(task_name, log_path, result, "")344	345	def test_48_getblockheight(self):346		log_path = "48_getblockheight.log"347		task_name = "48_getblockheight"348		self.start(log_path)349		stop_nodes([0, 1, 2, 3, 4, 5, 6])350		start_nodes([0, 1, 2, 3, 4, 5, 6], Config.DEFAULT_NODE_ARGS)351		time.sleep(10)352		(result, response) = wsapi.getblockheight()353		self.normal_finish(task_name, log_path, result, "")354	355	def test_49_getblockhashbyheight(self):356		log_path = "49_getblockhashbyheight.log"357		task_name = "49_getblockhashbyheight"358		self.start(log_path)359		(result, response) = wsapi.getblockhashbyheight(Conf.HEIGHT_CORRECT)360		self.normal_finish(task_name, log_path, result, "")361	def test_51_getblockhashbyheight(self):362		log_path = "51_getblockhashbyheight.log"363		task_name = "51_getblockhashbyheight"364		self.start(log_path)365		(result, response) = wsapi.getblockhashbyheight(Conf.HEIGHT_BORDER)366		self.normal_finish(task_name, log_path, result, "")367	def test_52_getblockhashbyheight(self):368		log_path = "52_getblockhashbyheight.log"369		task_name = "52_getblockhashbyheight"370		self.start(log_path)371		(result, response) = wsapi.getblockhashbyheight(Conf.HEIGHT_INCORRECT_1)372		self.abnormal_finish(task_name, log_path, result, "")373	def test_53_getblockhashbyheight(self):374		log_path = "53_getblockhashbyheight.log"375		task_name = "53_getblockhashbyheight"376		self.start(log_path)377		(result, response) = wsapi.getblockhashbyheight(Conf.HEIGHT_INCORRECT_2)378		self.abnormal_finish(task_name, log_path, result, "")379	def test_54_getblockhashbyheight(self):380		log_path = "54_getblockhashbyheight.log"381		task_name = "54_getblockhashbyheight"382		self.start(log_path)383		(result, response) = wsapi.getblockhashbyheight(Conf.HEIGHT_INCORRECT_3)384		self.abnormal_finish(task_name, log_path, result, "")385	def test_55_gettransaction(self):386		log_path = "55_gettransaction.log"387		task_name = "55_gettransaction"388		self.start(log_path)389		time.sleep(10)390		(result, response) = wsapi.gettransaction(Conf.TX_HASH_CORRECT)391		self.normal_finish(task_name, log_path, result, "")392	def test_56_gettransaction(self):393		log_path = "56_gettransaction.log"394		task_name = "56_gettransaction"395		self.start(log_path)396		(result, response) = wsapi.gettransaction(Conf.TX_HASH_INCORRECT_2)397		self.abnormal_finish(task_name, log_path, result, "")398	def test_57_gettransaction(self):399		log_path = "57_gettransaction.log"400		task_name = "57_gettransaction"401		self.start(log_path)402		(result, response) = wsapi.gettransaction(Conf.TX_HASH_INCORRECT_1)403		self.abnormal_finish(task_name, log_path, result, "")404	def test_58_gettransaction(self):405		log_path = "58_gettransaction.log"406		task_name = "58_gettransaction"407		self.start(log_path)408		(result, response) = wsapi.gettransaction(Conf.TX_HASH_INCORRECT_2)409		self.abnormal_finish(task_name, log_path, result, "")410	def test_59_gettransaction(self):411		log_path = "59_gettransaction.log"412		task_name = "59_gettransaction"413		self.start(log_path)414		(result, response) = wsapi.gettransaction(Conf.TX_HASH_INCORRECT_3)415		self.abnormal_finish(task_name, log_path, result, "")416	def test_60_gettransaction(self):417		log_path = "60_gettransaction.log"418		task_name = "60_gettransaction"419		self.start(log_path)420		(result, response) = wsapi.gettransaction(Conf.TX_HASH_INCORRECT_4)421		self.abnormal_finish(task_name, log_path, result, "")422	def test_61_sendrawtransaction(self):423		log_path = "61_sendrawtransaction.log"424		task_name = "61_sendrawtransaction"425		self.start(log_path)426		(result, response) = wsapi.sendrawtransaction(Conf.RAW_TRANSACTION_DATA_CORRECT)427		self.normal_finish(task_name, log_path, result, "")428	def test_63_sendrawtransaction(self):429		log_path = "63_sendrawtransaction.log"430		task_name = "63_sendrawtransaction"431		self.start(log_path)432		(result, response) = wsapi.sendrawtransaction(Conf.RAW_TRANSACTION_DATA_INCORRECT_2)433		self.abnormal_finish(task_name, log_path, result, "")434	def test_64_sendrawtransaction(self):435		log_path = "64_sendrawtransaction.log"436		task_name = "64_sendrawtransaction"437		self.start(log_path)438		(result, response) = wsapi.sendrawtransaction(Conf.RAW_TRANSACTION_DATA_INCORRECT_3)439		self.abnormal_finish(task_name, log_path, result, "")440	def test_65_sendrawtransaction(self):441		log_path = "65_sendrawtransaction.log"442		task_name = "65_sendrawtransaction"443		self.start(log_path)444		(result, response) = wsapi.sendrawtransaction(Conf.RAW_TRANSACTION_DATA_INCORRECT_1)445		self.abnormal_finish(task_name, log_path, result, "")446	def test_66_get_version(self):447		log_path = "66_get_version.log"448		task_name = "66_get_version"449		self.start(log_path)450		task = Task(Config.BASEAPI_PATH + "/ws/getversion.json")451		task.set_type("ws")452		param = None453		if param and isinstance(param, dict):454			taskrequest = task.request()455			for key in param:456				taskrequest[key] = param[key]457			task.set_request(taskrequest)458		(result, response) = run_single_task(task)459		self.normal_finish(task_name, log_path, result, "")460	def test_68_get_version(self):461		log_path = "68_get_version.log"462		task_name = "68_get_version"463		self.start(log_path)464		task = Task(Config.BASEAPI_PATH + "/ws/getversion.json")465		task.set_type("ws")466		param = {"height":""}467		if param and isinstance(param, dict):468			taskrequest = task.request()469			for key in param:470				taskrequest[key] = param[key]471			task.set_request(taskrequest)472		(result, response) = run_single_task(task)473		self.normal_finish(task_name, log_path, result, "")474	def test_69_get_version(self):475		log_path = "69_get_version.log"476		task_name = "69_get_version"477		self.start(log_path)478		task = Task(Config.BASEAPI_PATH + "/ws/getversion.json")479		task.set_type("ws")480		param = {"height":"abc"}481		if param and isinstance(param, dict):482			taskrequest = task.request()483			for key in param:484				taskrequest[key] = param[key]485			task.set_request(taskrequest)486		(result, response) = run_single_task(task)487		self.normal_finish(task_name, log_path, result, "")488	def test_70_getbalancebyaddr(self):489		log_path = "70_getbalancebyaddr.log"490		task_name = "70_getbalancebyaddr"491		self.start(log_path)492		(result, response) = wsapi.getbalancebyaddr(Conf.ACCOUNT_ADDRESS_CORRECT)493		self.normal_finish(task_name, log_path, result, "")494	def test_71_getbalancebyaddr(self):495		log_path = "71_getbalancebyaddr.log"496		task_name = "71_getbalancebyaddr"497		self.start(log_path)498		(result, response) = wsapi.getbalancebyaddr(Conf.ACCOUNT_ADDRESS_INCORRECT_2)499		self.abnormal_finish(task_name, log_path, result, "")500	def test_72_getbalancebyaddr(self):501		log_path = "72_getbalancebyaddr.log"502		task_name = "72_getbalancebyaddr"503		self.start(log_path)504		(result, response) = wsapi.getbalancebyaddr(Conf.ACCOUNT_ADDRESS_INCORRECT_1)505		self.abnormal_finish(task_name, log_path, result, "")506	def test_73_getbalancebyaddr(self):507		log_path = "73_getbalancebyaddr.log"508		task_name = "73_getbalancebyaddr"509		self.start(log_path)510		(result, response) = wsapi.getbalancebyaddr(Conf.ACCOUNT_ADDRESS_INCORRECT_3)511		self.abnormal_finish(task_name, log_path, result, "")512	def test_74_getcontract(self):513		log_path = "74_getcontract.log"514		task_name = "74_getcontract"515		self.start(log_path)516		(result, response) = wsapi.getcontract(Conf.ACCOUNT_ADDRESS_INCORRECT_4)517		self.abnormal_finish(task_name, log_path, result, "")518	def test_75_getcontract(self):519		log_path = "75_getcontract.log"520		task_name = "75_getcontract"521		self.start(log_path)522		time.sleep(10)523		(result, response) = wsapi.getcontract(Conf.CONTRACT_ADDRESS_CORRECT)524		self.normal_finish(task_name, log_path, result, "")525	def test_77_getcontract(self):526		log_path = "77_getcontract.log"527		task_name = "77_getcontract"528		self.start(log_path)529		time.sleep(5)530		(result, response) = wsapi.getcontract(Conf.CONTRACT_ADDRESS_INCORRECT_2)531		self.abnormal_finish(task_name, log_path, result, "")532	def test_78_getcontract(self):533		log_path = "78_getcontract.log"534		task_name = "78_getcontract"535		self.start(log_path)536		time.sleep(5)537		(result, response) = wsapi.getcontract(Conf.CONTRACT_ADDRESS_INCORRECT_1)538		self.abnormal_finish(task_name, log_path, result, "")539	def test_79_getcontract(self):540		log_path = "79_getcontract.log"541		task_name = "79_getcontract"542		self.start(log_path)543		time.sleep(5)544		(result, response) = wsapi.getcontract(Conf.CONTRACT_ADDRESS_INCORRECT_3)545		self.abnormal_finish(task_name, log_path, result, "")546	def test_80_getcontract(self):547		log_path = "80_getcontract.log"548		task_name = "80_getcontract"549		self.start(log_path)550		time.sleep(5)551		(result, response) = wsapi.getcontract(Conf.CONTRACT_ADDRESS_INCORRECT_4)552		self.abnormal_finish(task_name, log_path, result, "")553	def test_81_getsmartcodeeventbyheight(self):554		log_path = "81_getsmartcodeeventbyheight.log"555		task_name = "81_getsmartcodeeventbyheight"556		self.start(log_path)557		(result, response) = wsapi.getsmartcodeeventbyheight(Conf.HEIGHT_CORRECT)558		self.normal_finish(task_name, log_path, result, "")559	def test_82_getsmartcodeeventbyheight(self):560		log_path = "82_getsmartcodeeventbyheight.log"561		task_name = "82_getsmartcodeeventbyheight"562		self.start(log_path)563		(result, response) = wsapi.getsmartcodeeventbyheight(Conf.HEIGHT_CORRECT)564		self.normal_finish(task_name, log_path, result, "")565	def test_83_getsmartcodeeventbyheight(self):566		log_path = "83_getsmartcodeeventbyheight.log"567		task_name = "83_getsmartcodeeventbyheight"568		self.start(log_path)569		(result, response) = wsapi.getsmartcodeeventbyheight(Conf.HEIGHT_BORDER)570		self.normal_finish(task_name, log_path, result, "")571	def test_84_getsmartcodeeventbyheight(self):572		log_path = "84_getsmartcodeeventbyheight.log"573		task_name = "84_getsmartcodeeventbyheight"574		self.start(log_path)575		(result, response) = wsapi.getsmartcodeeventbyheight(Conf.HEIGHT_INCORRECT_1)576		self.normal_finish(task_name, log_path, result, "")577	def test_85_getsmartcodeeventbyheight(self):578		log_path = "85_getsmartcodeeventbyheight.log"579		task_name = "85_getsmartcodeeventbyheight"580		self.start(log_path)581		(result, response) = wsapi.getsmartcodeeventbyheight(Conf.HEIGHT_INCORRECT_2)582		self.normal_finish(task_name, log_path, result, "")583	def test_86_getsmartcodeeventbyheight(self):584		log_path = "86_getsmartcodeeventbyheight.log"585		task_name = "86_getsmartcodeeventbyheight"586		self.start(log_path)587		(result, response) = wsapi.getsmartcodeeventbyheight(Conf.HEIGHT_INCORRECT_3)588		self.abnormal_finish(task_name, log_path, result, "")589	def test_87_getsmartcodeeventbyhash(self):590		log_path = "87_getsmartcodeeventbyhash.log"591		task_name = "87_getsmartcodeeventbyhash"592		self.start(log_path)593		time.sleep(10)594		(result, response) = wsapi.getsmartcodeeventbyhash(Conf.TX_HASH_CORRECT)595		self.normal_finish(task_name, log_path, result, "")596	def test_88_getsmartcodeeventbyhash(self):597		log_path = "88_getsmartcodeeventbyhash.log"598		task_name = "88_getsmartcodeeventbyhash"599		self.start(log_path)600		time.sleep(10)601		(result, response) = wsapi.getsmartcodeeventbyhash(Conf.TX_HASH_INCORRECT_5)602		self.normal_finish(task_name, log_path, result, "")603	def test_89_getsmartcodeeventbyhash(self):604		log_path = "89_getsmartcodeeventbyhash.log"605		task_name = "89_getsmartcodeeventbyhash"606		self.start(log_path)607		time.sleep(10)608		(result, response) = wsapi.getsmartcodeeventbyhash(Conf.TX_HASH_INCORRECT_1)609		self.abnormal_finish(task_name, log_path, result, "")610	def test_90_getsmartcodeeventbyhash(self):611		log_path = "90_getsmartcodeeventbyhash.log"612		task_name = "90_getsmartcodeeventbyhash"613		self.start(log_path)614		time.sleep(10)615		(result, response) = wsapi.getsmartcodeeventbyhash(Conf.TX_HASH_INCORRECT_2)616		self.abnormal_finish(task_name, log_path, result, "")617	def test_91_getsmartcodeeventbyhash(self):618		log_path = "91_getsmartcodeeventbyhash.log"619		task_name = "91_getsmartcodeeventbyhash"620		self.start(log_path)621		time.sleep(10)622		(result, response) = wsapi.getsmartcodeeventbyhash(Conf.TX_HASH_INCORRECT_3)623		self.abnormal_finish(task_name, log_path, result, "")624	def test_92_getsmartcodeeventbyhash(self):625		log_path = "92_getsmartcodeeventbyhash.log"626		task_name = "92_getsmartcodeeventbyhash"627		self.start(log_path)628		time.sleep(10)629		(result, response) = wsapi.getsmartcodeeventbyhash(Conf.TX_HASH_INCORRECT_4)630		self.abnormal_finish(task_name, log_path, result, "")631	def test_93_getblockheightbytxhash(self):632		log_path = "93_getblockheightbytxhash.log"633		task_name = "93_getblockheightbytxhash"634		self.start(log_path)635		time.sleep(10)636		(result, response) = wsapi.getblockheightbytxhash(Conf.TX_HASH_CORRECT)637		self.normal_finish(task_name, log_path, result, "")638	def test_94_getblockheightbytxhash(self):639		log_path = "94_getblockheightbytxhash.log"640		task_name = "94_getblockheightbytxhash"641		self.start(log_path)642		time.sleep(10)643		(result, response) = wsapi.getblockheightbytxhash(Conf.TX_HASH_INCORRECT_5)644		self.abnormal_finish(task_name, log_path, result, "")645	def test_95_getblockheightbytxhash(self):646		log_path = "95_getblockheightbytxhash.log"647		task_name = "95_getblockheightbytxhash"648		self.start(log_path)649		time.sleep(10)650		(result, response) = wsapi.getblockheightbytxhash(Conf.TX_HASH_INCORRECT_1)651		self.abnormal_finish(task_name, log_path, result, "")652	def test_96_getblockheightbytxhash(self):653		log_path = "96_getblockheightbytxhash.log"654		task_name = "96_getblockheightbytxhash"655		self.start(log_path)656		time.sleep(10)657		(result, response) = wsapi.getblockheightbytxhash(Conf.TX_HASH_INCORRECT_2)658		self.abnormal_finish(task_name, log_path, result, "")					659	def test_97_getblockheightbytxhash(self):660		log_path = "97_getblockheightbytxhash.log"661		task_name = "97_getblockheightbytxhash"662		self.start(log_path)663		time.sleep(10)664		(result, response) = wsapi.getblockheightbytxhash(Conf.TX_HASH_INCORRECT_3)665		self.abnormal_finish(task_name, log_path, result, "")		666	def test_98_getblockheightbytxhash(self):667		log_path = "98_getblockheightbytxhash.log"668		task_name = "98_getblockheightbytxhash"669		self.start(log_path)670		time.sleep(10)671		(result, response) = wsapi.getblockheightbytxhash(Conf.TX_HASH_INCORRECT_4)672		self.abnormal_finish(task_name, log_path, result, "")673	def test_99_getmerkleproof(self):674		log_path = "99_getmerkleproof.log"675		task_name = "99_getmerkleproof"676		self.start(log_path)677		time.sleep(10)678		(result, response) = wsapi.getmerkleproof(Conf.TX_HASH_CORRECT)679		self.normal_finish(task_name, log_path, result, "")680	681	def test_100_getmerkleproof(self):682		log_path = "100_getmerkleproof.log"683		task_name = "100_getmerkleproof"684		self.start(log_path)685		time.sleep(10)686		(result, response) = wsapi.getmerkleproof(Conf.TX_HASH_INCORRECT_5)687		self.abnormal_finish(task_name, log_path, result, "")688	def test_101_getmerkleproof(self):689		log_path = "101_getmerkleproof.log"690		task_name = "101_getmerkleproof"691		self.start(log_path)692		time.sleep(10)693		(result, response) = wsapi.getmerkleproof(Conf.TX_HASH_INCORRECT_1)694		self.abnormal_finish(task_name, log_path, result, "")695	def test_102_getmerkleproof(self):696		log_path = "102_getmerkleproof.log"697		task_name = "102_getmerkleproof"698		self.start(log_path)699		time.sleep(10)700		(result, response) = wsapi.getmerkleproof(Conf.TX_HASH_INCORRECT_2)701		self.abnormal_finish(task_name, log_path, result, "")702	def test_103_getmerkleproof(self):703		log_path = "103_getmerkleproof.log"704		task_name = "103_getmerkleproof"705		self.start(log_path)706		time.sleep(10)707		(result, response) = wsapi.getmerkleproof(Conf.TX_HASH_INCORRECT_3)708		self.abnormal_finish(task_name, log_path, result, "")709	def test_104_getmerkleproof(self):710		log_path = "104_getmerkleproof.log"711		task_name = "104_getmerkleproof"712		self.start(log_path)713		time.sleep(10)714		(result, response) = wsapi.getmerkleproof(Conf.TX_HASH_INCORRECT_4)715		self.abnormal_finish(task_name, log_path, result, "")716	def test_105_getsessioncount(self):717		log_path = "105_getsessioncount.log"718		task_name = "105_getsessioncount"719		self.start(log_path)720		(result, response) = wsapi.getsessioncount()721		self.normal_finish(task_name, log_path, result, "")722	def test_106_getsessioncount(self):723		log_path = "106_getsessioncount.log"724		task_name = "106_getsessioncount"725		self.start(log_path)726		stop_node(0)727		(result, response) = wsapi.getsessioncount()728		start_node(0, Config.DEFAULT_NODE_ARGS)729		time.sleep(5)730		self.abnormal_finish(task_name, log_path, result, "")731	732	'''733	def test_107_getstorage(self):734		log_path = "107_getstorage.log"735		task_name = "107_getstorage"736		self.start(log_path)737		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_CORRECT, KEY_CORRECT)738		self.abnormal_finish(task_name, log_path, result, "")739	def test_108_getstorage(self):740		log_path = "108_getstorage.log"741		task_name = "108_getstorage"742		self.start(log_path)743		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_INCORRECT_2, KEY_CORRECT)744		self.normal_finish(task_name, log_path, result, "")745	def test_109_getstorage(self):746		log_path = "109_getstorage.log"747		task_name = "109_getstorage"748		self.start(log_path)749		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_INCORRECT_3, KEY_CORRECT)750		self.abnormal_finish(task_name, log_path, result, "")751	def test_110_getstorage(self):752		log_path = "110_getstorage.log"753		task_name = "110_getstorage"754		self.start(log_path)755		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_INCORRECT_4, KEY_CORRECT)756		self.abnormal_finish(task_name, log_path, result, "")757	def test_111_getstorage(self):758		log_path = "111_getstorage.log"759		task_name = "111_getstorage"760		self.start(log_path)761		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_INCORRECT_1, KEY_CORRECT)762		self.abnormal_finish(task_name, log_path, result, "")763	def test_112_getstorage(self):764		log_path = "112_getstorage.log"765		task_name = "112_getstorage"766		self.start(log_path)767		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_CORRECT, KEY_CORRECT)768		self.normal_finish(task_name, log_path, result, "")769	def test_113_getstorage(self):770		log_path = "113_getstorage.log"771		task_name = "113_getstorage"772		self.start(log_path)773		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_CORRECT, KEY_INCORRECT_2)774		self.abnormal_finish(task_name, log_path, result, "")775	def test_114_getstorage(self):776		log_path = "114_getstorage.log"777		task_name = "114_getstorage"778		self.start(log_path)779		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_CORRECT, KEY_INCORRECT_3)780		self.abnormal_finish(task_name, log_path, result, "")781	def test_115_getstorage(self):782		log_path = "115_getstorage.log"783		task_name = "115_getstorage"784		self.start(log_path)785		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_CORRECT, KEY_INCORRECT_4)786		self.abnormal_finish(task_name, log_path, result, "")787	def test_116_getstorage(self):788		log_path = "116_getstorage.log"789		task_name = "116_getstorage"790		self.start(log_path)791		(result, response) = wsapi.getstorage(CONTRACT_ADDRESS_CORRECT, KEY_INCORRECT_1)792		self.abnormal_finish(task_name, log_path, result, "")793	'''794####################################################795if __name__ == '__main__':...logger.py
Source:logger.py  
1import logging2import os3import pathlib4import sys5from logging import StreamHandler, Formatter6from logging.handlers import WatchedFileHandler7from .config import LOG_PATH, LOG_LEVEL8logging_name_to_Level = {9    'CRITICAL': logging.CRITICAL,10    'FATAL': logging.FATAL,11    'ERROR': logging.ERROR,12    'WARNING': logging.WARNING,13    'INFO': logging.INFO,14    'DEBUG': logging.DEBUG,15    'NOTSET': logging.NOTSET,16}17# def get_logger(log_path, log_level: str, name='mylogger'):18#     """19#     è·åæ¥å¿å®ä¾20#     Args:21#         log_path:    æ¥å¿ç®å½22#         log_level:   æ¥å¿çº§å«23#         name:        æ¥å¿è®°å½å¨çåå24#     Returns:25#     """26#     logger = logging.Logger(name)  # logging.getLogger(name) ä¼å¯¼è´å¤æ¬¡æå°27#     logger.setLevel(logging_name_to_Level.get(log_level.upper(), logging.INFO))28#     # å¯ä»¥å°æ¥å¿åå°ä»»æä½ç½®(handlers)29#     default_handlers = {30#         WatchedFileHandler(os.path.join(log_path, 'all.log')): logging.INFO,  # æææ¥å¿31#         WatchedFileHandler(os.path.join(log_path, 'error.log')): logging.ERROR,  # é误æ¥å¿32#         StreamHandler(sys.stdout): logging.DEBUG  # æ§å¶å°33#     }34#     # æ¥å¿æ ¼å¼ï¼[æ¶é´] [æä»¶å-è¡å·] [ç±»å] [ä¿¡æ¯]35#     _format = '%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] %(message)s - %(process)d-%(thread)d'36#     # æ·»å å¤ä¸ªä½ç½®37#     for handler, level in default_handlers.items():38#         handler.setFormatter(Formatter(_format))39#         if level is not None:40#             handler.setLevel(level)41#         logger.addHandler(handler)42#     return logger43# log_path = os.environ.get('LOG_PATH', "/data/logs")  # pylint: disable=C010344# # log_level = 'INFO'  # pylint: disable=C010345# log_path = pathlib.Path(log_path)46# if not log_path.exists():47#     log_path.mkdir(parents=True)48# logger = get_logger(log_path, LOG_LEVEL)49class LogUtil(logging.Logger):50    logging_name_to_Level = {51        'CRITICAL': logging.CRITICAL,52        'FATAL': logging.FATAL,53        'ERROR': logging.ERROR,54        'WARNING': logging.WARNING,55        'INFO': logging.INFO,56        'DEBUG': logging.DEBUG,57        'NOTSET': logging.NOTSET,58    }59    def __init__(self, log_path, log_level, name='mylogger'):60        super(LogUtil, self).__init__(name)61        # å建æ¥å¿ç®å½ï¼ä»ç¯å¢åéLOG_PATHè·åæ¥å¿ç®å½ï¼æ²¡æå°±ä½¿ç¨log_pathåæ°å¼62        log_path = pathlib.Path(os.environ.get("LOG_PATH") or log_path)63        if not log_path.exists():64            log_path.mkdir(parents=True)65        # 设置æ¥å¿çº§å«66        self.setLevel(self.logging_name_to_Level.get(log_level.upper(), logging.INFO))67        # æ¥å¿æ ¼å¼ï¼[æ¶é´]-[æ¥å¿çº§å«]-[æä»¶å-è¡å·]-[ä¿¡æ¯]-[è¿ç¨PID]-[线ç¨å·]68        _format = '%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] %(message)s - %(process)d-%(thread)d'69        # æ·»å æ¥å¿Handler70        default_handlers = {71            WatchedFileHandler(os.path.join(log_path, 'all.log')): logging.INFO,  # INFOæ¥å¿åå
¥all.logæä»¶72            WatchedFileHandler(os.path.join(log_path, 'error.log')): logging.ERROR,  # ERRORæ¥å¿åå
¥error.logæä»¶73            StreamHandler(sys.stdout): logging.DEBUG,  # æ§å¶å°è¾åºDEBUGä¿¡æ¯74            StreamHandler(sys.stderr): logging.ERROR   # æ§å¶å°è¾åºERRORä¿¡æ¯75        }76        for handler, level in default_handlers.items():77            handler.setFormatter(Formatter(_format))78            if level is not None:79                handler.setLevel(level)80            self.addHandler(handler)...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!!
