码迷,mamicode.com
首页 > 其他好文 > 详细

单元测试-从表格获取测试数据(3)

时间:2020-01-23 18:11:35      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:url   ror   col   表头   too   excel   val   end   inf   

# 1.get_data3.py
"""
把表头写进excel,获取表头和数据
"""

from openpyxl import load_workbook


class DoExcel:
    def __init__(self, file, sheet):
        self.file = file
        self.sheet = sheet
        self.sheet_obj = load_workbook(self.file)[self.sheet]

    def get_header(self):
        header = []
        for j in range(1, self.sheet_obj.max_column+1):
            header.append(self.sheet_obj.cell(1, j).value)
        return header

    def get_data(self):
        header = self.get_header()
        case_data = []
        for i in range(2, self.sheet_obj.max_row+1):
            sub_data = {}
            for j in range(1, self.sheet_obj.max_column+1):  # i=2, j=1\2\3\4
                sub_data[header[j-1]] = self.sheet_obj.cell(i, j).value
            case_data.append(sub_data)
        return case_data


if __name__ == "__main__":
    header = DoExcel("data_3.xlsx", "sh2").get_header()
    print(header, type(header))
    case_data = DoExcel("data_3.xlsx", "sh2").get_data()
    print(case_data, type(case_data))



# 2. class_test3.py

import unittest
from API_AUTO.tools.http_request import HttpRequest


class TestLogin(unittest.TestCase):
    def setUp(self):
        print("start testing...")

    def tearDown(self):
        print("case done")

    def __init__(self, methodName, url, method, data, expected):
        super(TestLogin, self).__init__(methodName)
        self.url = url
        self.method = method
        self.data = data
        self.expected = expected

    def test_api(self):
        res = HttpRequest().http_request(self.url, self.method, self.data)
        r = res.json()["info"]
        try:
            self.assertEqual(r, self.expected)
        except AssertionError as e:
            print("there is an error in the case: {}".format(e))
            raise e

# 3. run.py

import unittest
from day_20191202.para_3.class_test3 import TestLogin
from day_20191202.para_3.get_data3 import DoExcel
import HTMLTestRunner

suite = unittest.TestSuite()
case_data = DoExcel("data_3.xlsx", "sh2").get_data()
for item in case_data:
    suite.addTest(TestLogin("test_api", item["URL"], item["method"], eval(item["data"]), item["expected"]))
    print(eval(item["data"]), type(eval(item["data"])))
with open("login_3.html", "wb") as file:
    runner = HTMLTestRunner.HTMLTestRunner(stream=file,
                                           verbosity=2,
                                           title="登录3测试报告",
                                           description="加油,亲爱的自己")
    runner.run(suite)

# runner = unittest.TextTestRunner(verbosity=2)
# runner.run(suite)

 

表格测试数据如图:

技术图片

单元测试-从表格获取测试数据(3)

标签:url   ror   col   表头   too   excel   val   end   inf   

原文地址:https://www.cnblogs.com/come202011/p/12230898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!