pytest 打印调试信息
pytest_lean2.py
#coding=utf-8 import pytest import os import sys import time import json sys.path.append("/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])+"/lib") import requests sys.path.append("/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])) from util.getinfolib import getinfo import logging,sys log = logging.getLogger(__name__) class TestUM: ''' setup_class**********> setup_method##########>> setup----------> teardown----------> teardown_method##########>> teardown_class**********> ''' def setup(self): print ("setup---------->") def teardown(self): print ("teardown---------->") def setup_class(self): self.dic={} self.url_1=getinfo().go() self.headers={ 'Accept': 'application/json, text/plain, */*' ,'client': 'Web' ,'Content-Type': 'application/json;charset=UTF-8' ,'Origin': self.url_1 ,'Referer': self.url_1 ,'token': 'NGKPaLge8urbOlOAuHasURwYP4AKQIo8O1zad5F3vLA=' ,'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36' } print ("\nsetup_class**********>") def teardown_class(self): print ("teardown_class**********>") def setup_method(self, method): print ("setup_method##########>>") def teardown_method(self, method): print ("teardown_method##########>>") def get_company_id(self,url,type,companyName): payload={ "companyType":type, "companyStatus":"", "hasEnv":"", "hasDevice":"", "page": 1, "size": 20, "companyName":"" } r = requests.get(url, params=payload,headers=self.headers) assert r.status_code==200 assert r.json()['code']==200 company_list=r.json()["data"]['content'] for i in company_list: if(i["companyName"]==companyName): if(type==2): self.dic["waste_company_id"]=i["id"] elif(type==3): self.dic["consumer_company_id"]=i["id"] elif(type==4): self.dic["transport_company_id"]=i["id"] elif(type==5): self.dic["operate_company_id"]=i["id"] else: self.dic["charge_company_id"]=i["id"] break #1 kaptcha def test_kaptcha(self): url=self.url_1+"/login/kaptcha" payload = {} r = requests.get(url, params=payload) assert r.status_code==200 #2 login def test_login_pc(self): url=self.url_1+"/login/pc" payload = {"userName":"abc","passWord":"123","kaptcha":"12345"} r = requests.get(url, params=payload) self.dic["token"]=r.json()['data']['token'] self.dic["t"]=time.strftime("%Y%m%d%H%M%S", time.localtime()) self.headers["token"]=self.dic["token"] assert r.status_code==200 assert r.json()['code']==200 #3 add_waste_company def test_add_waste_company(self): url=self.url_1+"/companies" payload={"companyName":"test产废单位","companyType":2,"companyLinkman":"小明","companyPhone":"13111112222","address":{"province":330000,"city":330300,"area":330303,"street":330303006,"detail":"东京市红高粱村"},"gpsLocation":"130,40","email":"ewew@qq.com","businessLicenceUrl":r"http://test-icloudeep-img.oss-cn-beijing.aliyuncs.com/module/uploads/cd7288dfc50f4a25ba15929e685a7f10.jpeg","nearWater":1,"industryType":"mouldOfPlastics","companyStatus":"2",'companyDesc':'原料'} payload["companyName"]=payload["companyName"]+self.dic["t"] payload["companyLinkman"]=payload["companyLinkman"]+self.dic["t"] payload["companyDesc"]=payload["companyDesc"]+self.dic["t"] #带有嵌套的字典,需要转换为json传输 r = requests.post(url, data=json.dumps(payload),headers=self.headers) assert r.status_code==200 assert r.json()['code']==200 #获取单位ID self.get_company_id(url,payload["companyType"],payload["companyName"]) #4 add_trans_company def test_add_trans_company(self): url=self.url_1+"/companies" payload={"companyName":"test运输单位","companyType":4,"companyLinkman":"小明","companyPhone":"13123222122","address":{"province":330000,"city":330900,"area":330903,"street":330903003,"detail":"东京市红高粱村"},"gpsLocation":"132,26","email":"swded@qq.com","businessLicenceUrl":"http://test-icloudeep-img.oss-cn-beijing.aliyuncs.com/module/uploads/62c0eb0f821141cda92ffcec25d4208d.jpeg","remark":"备注"} payload["companyName"]=payload["companyName"]+self.dic["t"] payload["companyLinkman"]=payload["companyLinkman"]+self.dic["t"] payload["remark"]=payload["remark"]+self.dic["t"] #带有嵌套的字典,需要转换为json传输 r = requests.post(url, data=json.dumps(payload),headers=self.headers) assert r.status_code==200 assert r.json()['code']==200 #获取单位ID self.get_company_id(url,payload["companyType"],payload["companyName"]) #5 add_consumer_company def test_add_consumer_company(self): url=self.url_1+"/companies" payload={"companyName":"test处置单位","companyType":3,"companyLinkman":"小明","companyPhone":"13123222122","address":{"province":330000,"city":330800,"area":330803,"street":330803100,"detail":"东京市红高粱村"},"gpsLocation":"132,23","email":"wqwq@qq.com","businessLicenceUrl":"http://test-icloudeep-img.oss-cn-beijing.aliyuncs.com/module/uploads/c36c0e66b9954761a90aba9f5b19c71d.jpeg","remark":"备注","nearWater":"2"} payload["companyName"]=payload["companyName"]+self.dic["t"] payload["companyLinkman"]=payload["companyLinkman"]+self.dic["t"] payload["remark"]=payload["remark"]+self.dic["t"] #带有嵌套的字典,需要转换为json传输 r = requests.post(url, data=json.dumps(payload),headers=self.headers) assert r.status_code==200 assert r.json()['code']==200 #获取单位ID self.get_company_id(url,payload["companyType"],payload["companyName"]) #6 add_charge_company def test_add_charge_company(self): url=self.url_1+"/companies" payload={"companyName":"test监管单位","companyType":6,"companyLinkman":"小明","companyPhone":"13123222122","address":{"province":330000,"city":330700,"area":330727,"street":330727101,"detail":"东京市红高粱村"},"gpsLocation":"132,22","email":"swsq@qq.com","businessLicenceUrl":"http://test-icloudeep-img.oss-cn-beijing.aliyuncs.com/module/uploads/f448cc7669cf45eda56b6181d1bd20b7.jpeg","remark":"备注","adminArea":{"wholeCountry":"false","province":330000,"city":"","area":"","street":""}} payload["companyName"]=payload["companyName"]+self.dic["t"] payload["companyLinkman"]=payload["companyLinkman"]+self.dic["t"] payload["remark"]=payload["remark"]+self.dic["t"] #带有嵌套的字典,需要转换为json传输 r = requests.post(url, data=json.dumps(payload),headers=self.headers) assert r.status_code==200 assert r.json()['code']==200 #获取单位ID self.get_company_id(url,payload["companyType"],payload["companyName"]) #7 add_operate_company def test_add_operate_company(self): url=self.url_1+"/companies" payload={"companyName":"test运营单位","companyType":5,"companyLinkman":"小明","companyPhone":"13123222122","address":{"province":330000,"city":331100,"area":331122,"street":331122002,"detail":"东京市红高粱村"},"gpsLocation":"132,26","email":"deswaq@qq.com","businessLicenceUrl":"http://test-icloudeep-img.oss-cn-beijing.aliyuncs.com/module/uploads/7a4c448f9ec349a496d414440f42f09f.jpeg","remark":"备注","adminArea":{"wholeCountry":"false","province":330000,"city":330700,"area":"","street":""}} payload["companyName"]=payload["companyName"]+self.dic["t"] payload["companyLinkman"]=payload["companyLinkman"]+self.dic["t"] payload["remark"]=payload["remark"]+self.dic["t"] #带有嵌套的字典,需要转换为json传输 r = requests.post(url, data=json.dumps(payload),headers=self.headers) assert r.status_code==200 assert r.json()['code']==200 #获取单位ID self.get_company_id(url,payload["companyType"],payload["companyName"]) #8 add_wasteinfo_company def test_add_wasteinfo_company(self): url=self.url_1+"/companies/"+self.dic["waste_company_id"]+"/declarations/producer" payload={"wasteInfo":[{"wasteType":"HW09","wasteCode":"900-005-09","customWasteName":"废乳化液","yearDeclare":"20000","consumerId":"20191126200118230CP515590"}]} payload["wasteInfo"][0]["consumerId"]=self.dic["consumer_company_id"] #带有嵌套的字典,需要转换为json传输 r = requests.post(url,data=json.dumps(payload),headers=self.headers) log.info(r.json()) assert r.status_code==200 assert r.json()['code']==200
调用命令
pytest pytest_lean2.py -o log_cli=true -o log_cli_level=INFO
参考: