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

Posted come202011

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单元测试-从表格获取测试数据相关的知识,希望对你有一定的参考价值。

# 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=1234
                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)

 

表格测试数据如图:

技术图片

以上是关于单元测试-从表格获取测试数据的主要内容,如果未能解决你的问题,请参考以下文章

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

UI测试滑动删除表格视图单元格

在 Xcode ui 测试 Swift 中查找表格单元格

如何使用 IntentsTestRule 和 launchFragmentInContainer 测试从片段发送的意图

如何从 Visual Studio 2022 社区版的单元测试中获取代码覆盖率?

单元测试 NPE,当我添加片段自定义转换时