zoukankan      html  css  js  c++  java
  • Python--unittest参数化

    # file_to_list
    import os
    import xlrd
    import json


    class DataToParam(object):
    @classmethod
    def text(cls, file_name, seq=','):
    cls.file_exist(file_name)
    with open(file_name, encoding='utf-8') as f:
    res = []
    for line in f:
    res.append(line.strip().split(seq))
    return res

    @classmethod
    def excel(cls, file_name):
    cls.file_exist(file_name)
    book = xlrd.open_workbook(file_name) # 打开excel
    sheet = book.sheet_by_index(0) # 获取sheet页
    res = []
    for row in range(sheet.nrows):
    line_list = sheet.row_values(row) # 取excel里面的每一行数据,返回是一个list
    res.append(line_list)
    return res

    @classmethod
    def file_exist(cls, file_name):
    if os.path.isfile(file_name):
    return True
    raise Exception('参数化文件不存在!')
    # print(json.dumps(DataToParam.excel('data.xlsx'), indent=4))


    import unittest
    import nose_parameterized
    from file_to_list import DataToParam

    # case_data = [
    # [1, 1, 1.0],
    # [1, 2, 0.5],
    # [0.5, 1, 0.5]
    # ]


    def calc(a, b):
    a = int(a)
    b = int(b)
    res = round(a / b, 2)
    return res


    class MyTest(unittest.TestCase):
    @nose_parameterized.parameterized.expand(DataToParam.excel('data.xlsx'))
    def test_func(self, a, b, e):
    res = calc(a, b)
    self.assertEqual(res, float(e))


    if __name__ == '__main__':
    unittest.main() # 他会帮你运行当前这个python里面所有的测试用例
  • 相关阅读:
    Linux命令之vi
    CentOS7 查看IP
    Linux下mysql的命令
    @RequestMapping注解的参数说明
    springboot处理不同域的前端请求
    vue-cli4 取消关闭eslint 校验代码
    springmvc请求乱码
    访问静态资源报404错误
    eclipse创建Maven项目时pom.xml报错
    spring中的xml配置文件里报错
  • 原文地址:https://www.cnblogs.com/wangsilei/p/8572076.html
Copyright © 2011-2022 走看看