data driven application

Data driven:

Separate code blocks and data. In unittest, it is realized through ddt, ddt (data driver test data-driven test)

1. The first data-driven way to pass data parameters

 

 Pass a list collection through data, unpack unpack, and the parameter has a value

@ is python syntax, decorator

Multiple use cases can be executed together

2. The second method of data-driven transmission of test case data through .txt, if it is csv, etc., it is also in the form of open file

To be in the form of tuples, *, variable length parameters, unlimited number of parameters

* Interpret in the form of tuples

** Interpreted in the form of a dictionary

3. The data-driven third method can directly read the yaml file

Yaml installation: pip install pyyaml

 code:

#导入unittest包
import unittest
from ddt import ddt,data,unpack,file_data
#读文件的方法,放在类的外面
def read_file():
    file=open('data.txt','r',encoding='utf-8')
    li=[]
    #每行遍历
    for line in file.readlines():
    li.append(line.strip('\n').split(','))
    file.close()
return li
@ddt
class UnitDemo(unittest.TestCase):
    #前置条件
    #->Noneunittest更新以后默认在调用**补全,可以去掉不影响
    def setUp(self)->None:
        print('setUp')
    #后置条件
    deftearDown(self)->None:
        print('tearDown')
    #传入的参数是字典格式
    @file_data('data.yaml')
    def test_1(self,**user):
        username=user.get('username')
        password=user.get('password')
        #print(username+"+"+password)
        print(password)
        print(username)
if__name__=='__main__':
    #运行unittest测试用例
    #正常测试用例是前后关联的,如果不使用main函数调用,直接执行很可能会报错
    unittest.main()

 @ddt is indispensable, every data driver needs @ddt

 Why file_data does not need to add unpack

The role of unpack is to parse the list, list.split(',')

In the dictionary, the value can be found directly through the key, and the dictionary will not have the same value 

yaml file reading without ddt

1. Read the test data through the read_file() method, you can read .txt, save it to the list, analyze the value through the @data(*read_file()) function, and unpack the data twice through @unpack, and the unpacking is completed Then passed to the method to run

2. Use file_data to read the yaml file, @file_data('test.yaml'), so that no matter how many pieces of data there are in the yaml file, we can directly parse it through file_data without @unpack unpacking

yaml file read data operation:

code:

import yaml

file=open('test.yaml',encoding='UTF-8')
#loader=yaml.FullLoader必须要添加
res=yaml.load(file,Loader=yaml.FullLoader)

Note: python3 imports pyyaml, as shown below:

Use file_data to read yaml files, @file_data('test.yaml'),

The incoming parameter is in the form of a dictionary, and there is no need to add @unpack to unpack the data

@file_data is for reading files. If the method encapsulated in it reads a yaml file, it is the above open and yaml.load methods

If you read other forms of files, such as .txt, there may be other operations

Guess you like

Origin blog.csdn.net/Lynn1111111/article/details/122579892