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里面所有的测试用例
  • 相关阅读:
    聊一聊Flutter的setState()
    Flutter + Android 混合开发
    Flutter防止布局溢出
    Flutter获取远程数据 刷新UI界面
    Flutter日常笔记
    POJ 3299 Humidex
    POJ 1207 The 3n + 1 problem
    POJ 1005 I Think I Need a Houseboat
    POJ 1004 Financial Management
    POJ1012
  • 原文地址:https://www.cnblogs.com/wangsilei/p/8572076.html
Copyright © 2011-2022 走看看