unittest---ddt+Excel完成参数化

597 篇文章 17 订阅
19 篇文章 0 订阅

读取Excel

在这里插入图片描述1、安装xlrd

2、完成表格实例化

3、循环读取表格内容(这里安静把表格的内容分别放到一个列表中)方便后续通过ddt进行读取

import xlrd
def get_data(file_name):
    aa = []
    book = xlrd.open_workbook(file_name)
    sheet = book.sheet_by_index(0)
    for i in range(1, sheet.nrows):
        aa.append(list(sheet.row_values(i, 0, sheet.ncols)))
    return aa
result = get_data('123.xlsx')
print(result)

打印结果:

[['上海', '331eab8f3481f37868378fcdc76cb7cd', '上海'], ['北京', '331eab8f3481f37868378fcdc76cb7cd', '北京'], ['广州', '331eab8f3481f37868378fcdc76cb7c', '错误的请求KEY']]

Ddt+Excel

上面的excel中的数据已经得到了,我们需要把数据和ddt结合在一起,这里安静拿前面的接口进行调试

1、封装接口内容

2、导入unittests模块

3、通过ddt读取excel结果

import ddt
import requests
import unittests

@ddt.ddt
class Test(unittest.TestCase):
    def select(self,city,key):
        url = 'http://apis.juhe.cn/simpleWeather/query'
        data = {
            "city":city,
            "key":key
        }
        r = requests.post(url,data=data)
        return r

    @ddt.data(*result)
    @ddt.unpack
    def test_01(self,city,key,cake):
        result = self.select(city,key)
        print(result.text)
        self.assertIn(cake,result.text)


if __name__ == '__main__':
    unittest.main(verbosity=2)

打印结果:

test_01_1___上海____331eab8f3481f37868378fcdc76cb7cd____上海__ (__main__.Test) ... ok
{"reason":"查询成功!","result":{"city":"上海","realtime":{"temperature":"17","humidity":"49","info":"晴","wid":"00","direct":"东北风","power":"1级","aqi":"30"},"future":[{"date":"2020-11-11","temperature":"13\/19℃","weather":"晴","wid":{"day":"00","night":"00"},"direct":"东风转东北风"},{"date":"2020-11-12","temperature":"14\/21℃","weather":"多云转晴","wid":{"day":"01","night":"00"},"direct":"东风转东北风"},{"date":"2020-11-13","temperature":"14\/20℃","weather":"晴转多云","wid":{"day":"00","night":"01"},"direct":"东北风"},{"date":"2020-11-14","temperature":"15\/20℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"东北风"},{"date":"2020-11-15","temperature":"17\/21℃","weather":"阴转小雨","wid":{"day":"02","night":"07"},"direct":"东风"}]},"error_code":0}
test_01_2___北京____331eab8f3481f37868378fcdc76cb7cd____北京__ (__main__.Test) ... ok
{"reason":"查询成功!","result":{"city":"北京","realtime":{"temperature":"14","humidity":"42","info":"霾","wid":"53","direct":"西南风","power":"1级","aqi":"186"},"future":[{"date":"2020-11-11","temperature":"4\/16℃","weather":"晴转多云","wid":{"day":"00","night":"01"},"direct":"南风转北风"},{"date":"2020-11-12","temperature":"3\/19℃","weather":"晴","wid":{"day":"00","night":"00"},"direct":"北风"},{"date":"2020-11-13","temperature":"3\/15℃","weather":"晴转多云","wid":{"day":"00","night":"01"},"direct":"东南风转南风"},{"date":"2020-11-14","temperature":"5\/14℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"南风转北风"},{"date":"2020-11-15","temperature":"6\/15℃","weather":"多云转小雨","wid":{"day":"01","night":"07"},"direct":"北风转东风"}]},"error_code":0}
test_01_3___广州____331eab8f3481f37868378fcdc76cb7c____错误的请求KEY__ (__main__.Test) ... ok
{"resultcode":"101","reason":"错误的请求KEY","result":null,"error_code":10001}

----------------------------------------------------------------------
Ran 3 tests in 0.414s

OK

完整代码

import xlrd
import unittest
import ddt
import requests

def get_data(file_name):
    aa = []
    book = xlrd.open_workbook(file_name)
    sheet = book.sheet_by_index(0)
    for i in range(1, sheet.nrows):
        aa.append(list(sheet.row_values(i, 0, sheet.ncols)))
    return aa
result = get_data('123.xlsx')
@ddt.ddt
class Test(unittest.TestCase):
    # 封装接口
    def select(self,city,key):
        url = 'http://apis.juhe.cn/simpleWeather/query'
        data = {
            "city":city,
            "key":key
        }
        r = requests.post(url,data=data)
        return r

    @ddt.data(*result)
    @ddt.unpack
    # 进行参数化
    def test_01(self,city,key,cake):
        result = self.select(city,key)
        print(result.text)
        self.assertIn(cake,result.text)

if __name__ == '__main__':
    # 通过verbosity=2 查看详细内容
    unittest.main(verbosity=2)

在这里插入图片描述
希望本文对你有所帮助~~如果对软件测试、接口测试、自动化测试、面试经验交流感兴趣可以加入我们。642830685,免费领取最新软件测试大厂面试资料和Python自动化、接口、框架搭建学习资料!技术大牛解惑答疑,同行一起交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值