DDT test data driver Python3-unittest framework

DDT test data driver unittest framework

Use of ddt

DDT data-driven

  • DDT: Data Driver Test (data-driven test)
  • The data driver thought: separating data and use cases, external data to generate test cases

installation

pip install ddt

Ddt modified source (with each embodiment is to display the title corresponding to the title of the report generator)

  1. ddt ddt file () method
  2. The original test_data_docstring = _get_test_data_docstring (func, v) commented
  3. If the data is subject to the saved, add test_data_docstring = v.title
  4. If the data using a dictionary to store, just add test_data_docstring = v [ "title"]
  5. Generated after test report will be able to modify the title display column to the test report
  6. Note: If the column name title Excel table
def ddt(cls):
    for name, func in list(cls.__dict__.items()):
        if hasattr(func, DATA_ATTR):
            for i, v in enumerate(getattr(func, DATA_ATTR)):
                test_name = mk_test_name(name, getattr(v, "__name__", v), i)
                # test_data_docstring = _get_test_data_docstring(func, v) # 原来的
                # test_data_docstring = v.title  # 数据使用对象来保存,改成这样
                test_data_docstring = v["title"]  # 数据使用字典来保存,改成这样
                if hasattr(func, UNPACK_ATTR):...
                else:
                    add_test(cls, test_name, test_data_docstring, func, v)
            delattr(cls, name)
        elif hasattr(func, FILE_ATTR):...
    return cls

Using the data acquired in the data driver ddt test class (in this case a test class requires only one test method can be used)

First prepare a simple login function function

def login(name=None, pwd=None):
    if name and pwd:
        if name == "desire" and pwd == "123456":
            return {"code": 0, "msg": "登陆成功"}
        else:
            return {"code": 1, "msg": "账号密码错误"}
    else:
        return {"code": 1001, "msg": "不能有空值"}

Ready to excel table test

Import module ddt

from ddt import ddt, data

Create a test case class, plus ddt decorator @ddt

Read excel spreadsheet ReadExcel

@ddt
class LoginTestCase(unittest.TestCase):
    # DATA_DIR:测试用例存放的路径,用os模块进行拼接excel用例表格的路径
    excel_path = os.path.join(DATA_DIR, 'cases.xlsx')
    login = ReadExcel(excel_path, "login")
    # 读取数据(字典形式)
    login_datas = login.read_data_dict()
    # 读取数据(类的形式)
    # login_datas = login.read_data_obj()

Create a test case method, together with ddt decorator @data

@Data parameter () to pass inside the unpacking, the method in each case the data to the parameter
    @data(*login_datas)
    def test_login(self, case):
        pass
Step 1: Preparation Example data
①: a dictionary value (ddt need to modify the source file to use a dictionary)
        # 1.参数
        login_data = eval(case["data"])
        # 2.预期结果
        expected = eval(case["expected"])
        # 3.用例序号
        case_id = case["case_id"]
②: the form of the value type (ddt need to modify the source file using the class form)
        # 1.参数
        login_data = eval(case.data)
        # 2.预期结果
        expected = eval(case.expected)
        # 3.用例序号
        case_id = case.case_id
The second step: the implementation of performance function to get the actual result
        result = login(*login_data)
The third step: The expected and actual results assertion comparison
        try:
            self.assertEqual(expected, result)
        except AssertionError as a:
            # 把用例未通过结果写入excel中(调用[ReadExcel](https://www.cnblogs.com/desireyang/p/12059916.html)类中的写入方法)
            self.login.write_data(row=case_id + 1, column=5, value="用例未通过")
            raise a
        else:
            self.login.write_data(row=case_id + 1, column=5, value="用例通过")
Can directly run the test case class, automated test results will be written to excel in

Guess you like

Origin www.cnblogs.com/desireyang/p/12091154.html