標籤:setup err accept alert param ror 測試 ati http
開始代碼如下,產生報告內容為空白:
#coding=utf-8
import unittest,time,re
import requests
import json
import HTMLTestRunner
class test_api(unittest.TestCase):
def setUp(self):
self.url = "http://10.10.10.92:5050/api/demo/add.do"
self.params1 = {‘id‘:‘10011‘}
self.headers = {‘content-type‘:‘application/json‘}
self.verificationErrors = []
self.accept_next_alert = True
def test_aad_api(self):
req = requests.post(self.url,data=json.dumps(self.params1),headers=self.headers)
status = req.status_code
print(req.json())
if status == 200:
print("添加api介面成功")
else:
print("添加API介面失敗!")
def tearDown(self):
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
#取前面時間
now = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
filename = ‘D:\\python-function\\api\\report\\‘+now+‘result.html‘
fp = open(filename, ‘wb‘)
#定義測試報告
runner =HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u‘API測試報告‘,
description=u‘用例執行情況:‘)
#執行測試案例
testsuite = unittest.TestSuite()
#添加測試案例到測試集中
testsuite.addTest(test_api("test_aad_api"))
runner.run(testsuite)
百思不得其解,想了好久,最後是發現每次產生的報告檔案都刪除不了,然後在後面加了一個,fp.close(),在執行,報告有內容了,希望能夠協助到跟我一樣的人,完整代碼如下:
#coding=utf-8
import unittest,time,re
import requests
import json
import HTMLTestRunner
class test_api(unittest.TestCase):
def setUp(self):
self.url = "http://10.10.10.92:5050/api/demo/add.do"
self.params1 = {‘id‘:‘10011‘}
self.headers = {‘content-type‘:‘application/json‘}
self.verificationErrors = []
self.accept_next_alert = True
def test_aad_api(self):
req = requests.post(self.url,data=json.dumps(self.params1),headers=self.headers)
status = req.status_code
print(req.json())
if status == 200:
print("添加api介面成功")
else:
print("添加API介面失敗!")
def tearDown(self):
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
#取前面時間
now = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
filename = ‘D:\\python-function\\api\\report\\‘+now+‘result.html‘
fp = open(filename, ‘wb‘)
#定義測試報告
runner =HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u‘API測試報告‘,
description=u‘用例執行情況:‘)
#執行測試案例
testsuite = unittest.TestSuite()
#添加測試案例到測試集中
testsuite.addTest(test_api("test_aad_api"))
runner.run(testsuite)
fp.close()
這樣報告就有內容了
調用HTMLTestRunner生產的報告內容為空白解決辦法