码迷,mamicode.com
首页 > 编程语言 > 详细

接口自动化1.0 python+unittest

时间:2019-12-06 18:51:25      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:osi   upload   技术   login   容器   return   ddt   处理   runner   

自己尝试拿自己测试的系统编写接口自动化,不断优化,不断完善,记录学习过程,以及过程中遇到的问题及解决办法。

采用的结构是python+unittest,数据驱动模式

代码的结构:

技术图片

 

 测试数据:

技术图片

 

 处理测试数据,使用tool里面的get_data.py,代码:

import openpyxl


class Do_Excel:
def __init__(self, file_name, sheet_name):
self.file_name = file_name
self.sheet_name = sheet_name

def get_data(self):
wb = openpyxl.load_workbook(self.file_name)
sheet = wb[self.sheet_name]
test_data = []
list_data = []
for j in range(1, sheet.max_column + 1):
for i in range(1, sheet.max_row + 1):
list_data.append(sheet.cell(j, i).value)
test_data.append(list_data)
list_data = []
res = [dict(zip(test_data[0], test_data[i])) for i in range(1, len(test_data))]
return res


if __name__ == ‘__main__‘:
res = Do_Excel(‘test_data.xlsx‘, ‘python‘).get_data()
print(res[0][‘url‘])

测试用例test_cases下的test_formcenter.py 代码:
import unittest
import requests
from gongdan.tools.get_data import Do_Excel
from gongdan import login


class TestForm(unittest.TestCase):
def setUp(self):
self.headers = {"Content-Type": "application/json",
‘Accept‘: "application/json, text/plain, */*",
"passport": login.login()
}

def test_create_groups(self):
test_data = Do_Excel(r‘../test_data/test_data.xlsx‘, ‘python‘).get_data()
url = test_data[0][‘url‘]
data = eval(test_data[0][‘data‘])
res = requests.post(url, json=data,headers=self.headers)
print(res.text)
self.assertEqual(200, res.status_code)

def test_create_form(self):
test_data = Do_Excel(r‘../test_data/test_data.xlsx‘, ‘python‘).get_data()
url = test_data[1][‘url‘]
data = eval(test_data[1][‘data‘])
res = requests.post(url, json=data, headers=self.headers)
print(res.text)
self.assertEqual(200, res.status_code)

执行测试用例,do_cases下的run_cases,代码:
import unittest
import HTMLTestRunnerNew
from gongdan.test_cases import test_formcenter

#创建容器
suite = unittest.TestSuite()

#创建加载器
loader = unittest.TestLoader()

#加载用例
suite.addTests(loader.loadTestsFromModule(test_formcenter))

with open(r‘D:/todo/gongdan/test_rusult/form.html‘,‘wb‘) as f :
runner = HTMLTestRunnerNew.HTMLTestRunner(stream=f,verbosity=2,title="表单中心")
runner.run(suite)

测试报告展示:

技术图片

 技术图片

接口自动化1.0 python+unittest

标签:osi   upload   技术   login   容器   return   ddt   处理   runner   

原文地址:https://www.cnblogs.com/thcly/p/11996771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!