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

Posted mr-zy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unittest管理接口用例(数据分离-读取excel)相关的知识,希望对你有一定的参考价值。

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_docpycharm2python_API\result\report.html"
    path = open(dir,"wb")
    runner =HTMLTestRunner.HTMLTestRunner(stream=path,title="TestReport",description="test desc")
    runner.run(suit)

 

 

 

 

 

 

以上是关于unittest管理接口用例(数据分离-读取excel)的主要内容,如果未能解决你的问题,请参考以下文章

python 接口自动化测试使用unittest 批量用例管理

接口测试基础-ddt

接口测试基础-ddt

python接口自动化测试-unittest-单个用例管理

Python+unittest+DDT实现的数据驱动测试

python+ddt+unittest+excel