当前位置:   article > 正文

python执行接口测试case_python+request+unittest+HTMLTestRunner代码接口测试实例

一个接口 可能需要调用多个接口 这种用python脚本怎么去写 case

步骤:

1.把每个接口写成一个个测试脚本

2.一个测试脚本中包含一个接口,但是可以包含多个测试用例(即每个接口需要进行多种情况的验证,接口测试用例名称已test开头)

3.用testsuite直接调用这些接口类,构造测试集;或利用unittest自动识别测试用例

4.利用HTMLTestRunner生成测试报告

单个接口例子:

import requests

import json

def test_qualification_add():

url = "http://xxx.xxx.xxx/audit/api/xxx/get" #测试的接口url

headers = {"Content-Type":"application/json"}

data = {#接口传送的参数

"token": "abcdefg",

"id": 1,

"param": {

"QuId":1

}

}

r = requests.post(url = url,json = data,headers = headers) #发送请求

return r.json

print (r.text) #获取响应报文

print (r.status_code)

ifname=="main":

test_qualification_add()

测试用例封装为接口类:

import requests

import json

import unittest

class MyTest(unittest.TestCase): #封装测试环境的初始化和还原的类

def setUp(self): #放对数据可操作的代码,如对mysql、momgodb的初始化等,这里不对数据库进行操作!

print("start test")

pass

def tearDown(self): #与setUp()相对

print("end test")

pass

class test_xxx_get(MyTest): #把这个接口封装一个类,下面的方法是具体的测试用例

'''''接口名称:获取资质''' #这个描述接口名称

def test_xxx_get(self):

'''''测试用例1:哈哈''' #这个描述接口用例名称

self.url = "http://xxx.xxx.xxx/audit/api/xxx/get" #请求url

self.headers = {"Content-Type":"application/json"}

self.data = { #请求参数

"token": "abcdefg",

"id": 1,

"param": {

"QuId": 14

}

} #self.用在方法属性中,表示是该方法的属性,不会影响其他方法的属性。

r = requests.post(url = self.url,json = self.data,headers = self.headers)

return r.json()

print (self.r.text)

print (self.r.status_code)

self.assertIn("true",self.r.text) #断言判断接口返回是否符合要求,可以写多个断言!

if name=="main":

unittest.main()

unittest提供了全局的main()方法,使用它可以方便的将一个单元测试木块变成可以直接运行的测试脚本;

main()方法使用TestLoader类来搜索所有包含在该木块中以“test”命名开头的测试方法,并自动执行它;

运行所有测试用例:

import unittest

import json

import requests

from HTMLTestRunner import HTMLTestRunner

import time #加载测试文件 (有几多个接口就加载几多个,一个一个加进来)

import test_creative_add

import test_creative_get

import test_qualification_add

import test_qualification_get

import test_qualification_reflesh

testsuit#构造测试集

suite = unittest.TestSuite() #实例化

.#TestSuite类的addTest()方法把不同测试类中的测试方法组装到测试套件中。

.#增加测试用例==》接口文件名.接口类(方法也就是这个接口的其他用例),要把每一个测试用例都增加进来!!!

suite.addTest(test_creative_add.test_creative_add("test_creative_add")) #增加创意

suite.addTest(test_creative_get.test_creative_get("test_creative_get")) #获取创意

suite.addTest(test_qualification_add.test_qualification_add("test_qualification_add"))#增加资质

suite.addTest(test_qualification_get.test_qualification_get("test_qualification_get"))#获取资质

suite.addTest(test_qualification_reflesh.test_qualification_reflesh("test_qualification_reflesh"))#更新资质

if name=="main":

testunit = unittest.TestSuite()

testunit.addTest(suite)

#按照一定的格式获取当前的时间

now = time.strftime("%Y-%m-%d %H_%M_%S")

#定义报告存放路径

filename = './' + now + 'test_result.html'

fp = open(filename,"wb")

#定义测试报告

runner = HTMLTestRunner(stream = fp,

title = "xxx接口测试报告",

description = "测试用例执行情况:")

#运行测试

runner.run(testunit)

fp.close() #关闭文件对象把数据写进磁盘

假如有很多用例,不想一次次添加构造集。可以使用discover函数

import unittest

import json

import requests

from HTMLTestRunner import HTMLTestRunner

import time

.#定义测试用例的目录为当前目录

test_dir = './'

discover = unittest.defaultTestLoader.discover(test_dir,pattern = 'test*.py')

if name=="main":

#按照一定的格式获取当前的时间

now = time.strftime("%Y-%m-%d %H-%M-%S")

#定义报告存放路径

filename = './' + now + 'test_result.html'

fp = open(filename,"wb")

#定义测试报告

runner = HTMLTestRunner(stream = fp,

title = "xxx接口测试报告",

description = "测试用例执行情况:")

#运行测试

runner.run(discover)

fp.close() #关闭报告文件

发送测试报告到固定邮箱:

import unittest

import requests

from HTMLTestRunner import HTMLTestRunner

import time

import os

import smtplib

from email.mime.text import MIMEText

from email.header import Header

======定义发送邮件========

def send_mail(file_new):

f = open(file_new,'rb')

mail_body = f.read()

f.close()

msg = MIMEText(mail_body,'html','utf-8')

msg['Subject'] = Header('xxx接口自动化测试报告','utf-8')

smtp = smtplib.SMTP()

smtp.connect('smtp.sina.com')

smtp.login('xxx@sina.com','xxx336..')

smtp.sendmail('xxx@sina.com','10xxx6@qq.com',msg.as_string())

smtp.quit()

print('邮件已发出!注意查收。')

======查找测试目录,找到最新生成的测试报告======

def new_report(test_report):

lists = os.listdir(test_report)

lists.sort(key=lambda fn:os.path.getmtime(test_report + '\' + fn))

file_new = os.path.join(test_report,lists[-1])

print(file_new)

return file_new

if name == "main":

test_dir = "D:\dsp_testpro\test_case"

test_report = "D:\dsp_testpro\test_report"

discover = unittest.defaultTestLoader.discover(test_dir,

pattern = 'test*.py')

#按照一定的格式获取当前的时间

now = time.strftime("%Y-%m-%d_%H-%M-%S-")

#定义报告存放路径

filename = test_report + "\\" + now + 'result.html'

fp = open(filename,'wb')

#定义测试报告

runner = HTMLTestRunner(stream = fp,

title = "xxx接口测试报告",

description = "测试用例执行情况:")

#运行测试

runner.run(discover)

fp.close() #关闭报告文件

new_report = new_report(test_report)

send_mail(new_report)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/249853
推荐阅读
相关标签
  

闽ICP备14008679号