运行代码如下:
import unittest,time
from BSTestRunner import BSTestRunner
# 接口测试报告生成(BSTestRunner)的下载地址:https://github.com/easonhan007/HTMLTestRunner
# 下载完后放到python的安装路径下的Lib目录下
# 指定测试用例和测试报告的路径
test1='./tqwtest'
test2='./tqwbg'
# 加载测试用例
dic=unittest.defaultTestLoader.discover(test1,pattern='unitest_tq.py')
# 定义报告的文件格式
now=time.strftime("%Y-%m-%d %H_%M_%S")
csbgname=test2+'/'+now+'csbg.html'
#运行并生成测试报告
with open(csbgname,'wb') as f:
runner=BSTestRunner(stream=f,title="测试报告",description="这个是测试报告说明")
runner.run(dic)
用例代码如下:
import unittest,requests,xmltodict,json
from time import sleep
from urllib import parse
class TQ(unittest.TestCase):
def setUp(self):
self.url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"
self.headr={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
# self.prox = {"http": "http://113.101.158.208:4216"}
# def test_Tq_ok(self):
# city = {"theCityName": "上海"}
# city=parse.urlencode(city).encode('utf-8')
# r=requests.get(self.url,headers=self.headr,params=city)#,proxies=self.prox
# rdata=xmltodict.parse(r.text)#将xml转成str
# rjson=json.dumps(rdata,ensure_ascii=False,indent=1)#将str转换成json
# rjson=eval(rjson)#将json转换成dict格式
# self.assertEqual(rjson['ArrayOfString']['@xmlns:xsi'],'http://www.w3.org/2001/XMLSchema-instance')
def test_Tq_cscw(self):
city = {"theCityName": "123"}
city = parse.urlencode(city).encode('utf-8')
r = requests.get(self.url, headers=self.headr, params=city)
rdata = xmltodict.parse(r.text)
rjson = json.dumps(rdata, ensure_ascii=False, indent=1)
data=rjson.replace('null','404')
data = eval(data)
print(data)
self.assertEqual(data['ArrayOfString']['string'][0], '查询结果为空!')
sleep(3)
if __name__=='__main__':
unittest.main()
运行成功的结果如下:
用例失败的结果: