码迷,mamicode.com
首页 > 其他好文 > 详细

unittest管理接口用例(数据分离-读取excel)

时间:2019-11-17 17:31:04      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:utf-8   tin   pat   path   get   response   body   name   header   

1.简单读取

#coding=utf-8

#调用封装好的excel读取公共方法
from python_API.common.ReadExcel import ReadExcel
import requests
import json

#获取excel中的url
url = ReadExcel("d:\\dym.xls","Sheet1").getValue(1,1)

#获取excel中的请求方式
Method = ReadExcel("d:\\dym.xls","Sheet1").getValue(1,2)

#获取excel中的header
header = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(1,3))

#获取excel中的param
body = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(1,4))

response = requests.request(Method,url,headers=header,params=body)
print response.json()

 

2.加上unitest框架管理用例生成测试报告

#coding=utf-8

from python_API.common.ReadExcel import ReadExcel
import requests
import json
import unittest
import HTMLTestRunner

class Test(unittest.TestCase):

    def setUp(self):
        self.url = ReadExcel("d:\\dym.xls","Sheet1").getValue(1,1)
        self.Method = ReadExcel("d:\\dym.xls","Sheet1").getValue(1,2)
        self.header = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(1,3))

    def test01(self):
        body = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(1,4))
        response = requests.request(self.Method,self.url,headers=self.header,params=body)
        self.assertEqual(response.json()["values"]["loginName"],"17779828887",msg="test01 error!")

    def test02(self):
        body = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(2,4))
        response = requests.request(self.Method,self.url,headers=self.header,params=body)
        self.assertIn(u"错误",response.json()["errorMsg"],msg="test02 error!")

    def test03(self):
        body = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(3,4))
        response = requests.request(self.Method,self.url,headers=self.header,params=body)
        self.assertIn(u"错误",response.json()["errorMsg"],msg="test02 error!")

if __name__ == __main__:
    suit = unittest.TestSuite()
    testcases = [Test("test01"),Test("test02"),Test("test03")]
    suit.addTests(testcases)
    dir = "D:\work_doc\pycharm2\python_API\\result\\report.html"
    path = open(dir,"wb")
    runner =HTMLTestRunner.HTMLTestRunner(stream=path,title="TestReport",description="test desc")
    runner.run(suit)

 

 

 

 

 

 

unittest管理接口用例(数据分离-读取excel)

标签:utf-8   tin   pat   path   get   response   body   name   header   

原文地址:https://www.cnblogs.com/Mr-ZY/p/11877062.html

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