Various methods of data-driven testing for automated testing


Preface

`

The so-called data-driven testing is to drive the execution of automated tests by changing data, and ultimately obtain different test results. Simply put, data-driven is the parameterization of data, which causes different outputs due to different inputs.

Using data driver can enhance the reusability of scripts, realize the separation of data and scripts, that is, implement the PO model, which can greatly simplify the maintenance of automated scripts.

There are many data-driven methods, which can be divided into two types. One is to read defined arrays and dictionaries, and the second is to obtain data by reading external files (excel, csv, txt, xml, etc.).


1. Read the dictionary of definitions

This method is to directly define the data in the python or yaml file, import the file, and then obtain the data in the dictionary.

1. Define data in python file

As shown in the figure below, different parameters of the background login interface are defined in the python file.
python parameter definition
Get the parameters in the use case and define different test cases.
Insert image description here

2. Read external files

1. Fill in the use case parameters corresponding to each page in the Excel file, as follows

Insert image description here

2. Define a public method class to obtain Excel file parameters

Insert image description here

code show as below:

import xlrd

# 通过excel文件地址获取excel文件内容,返回为list列表
def read_excel_datas(fileAddr: str, sheetName:str) -> list:
    """
    通过excel文件地址获取excel文件内容(所有内容)
    :param sheetName: sheet的名字
    :param fileAddr: 文件地址
    :return: list内容
    """
    rDatas = []
    fileData = xlrd.open_workbook(fileAddr)

    sheetLen = len(fileData.sheets())
    for i in range(sheetLen):
        datas = []
        sheet = fileData.sheet_by_name(sheetName)
        for item in range(sheet.nrows):
            if item == 0:
                continue
            datas.append(sheet.row_values(item))
        rDatas.append(datas)
    return rDatas[0]

Summarize

The above is what I am going to talk about today. This article briefly introduces two methods of data-driven testing to provide some inspiration. If you have a better way, please leave a message to discuss.

Guess you like

Origin blog.csdn.net/liangxiaoyan0426/article/details/130871024