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

  前面介绍了几种参数化内容,有ddt,paramunittest,ddt+yaml等例子,今天安静在来介绍一种方法ddt+Excel的方法进行完成数据参数化

读取Excel

安静以前写了一篇通过python读取excel的文章,里面简单的介绍了如何读取excel和写入excel内容。文章地址:python读写Excel方法(xlwt和xlrd)

稳固而执行,可以为师也。一起温习一遍。

这里安静还是那接口来写了,先写个接口需要的参数内容

unittest---ddt+Excel完成参数化_第1张图片

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)

本篇主要用到了python读取Excel内容以及ddt的使用方法,学习了这个我们也可以举一反三,通过读取数据库内容,然后在通过ddt的形式进行参数化。

如果安静写的对您有帮助,点个关注,持续更新~~有什么不懂的或者写错的地方可以在下方进行评论留言,安静看到后第一时间进行回复。

你可能感兴趣的:(excel,python,开发语言)