Proficient in automation from 0 to 1, interface automation testing - data-driven DDT combat


foreword

Introduction to DDT

Name: Data-Driven Tests, Data-Driven Test
Function: The execution of test cases is driven by a collection of external data
Core idea: Data and test code separation
Application scenario: A set of external data to perform the same operation
Advantages: When a large amount of test data occurs In the case of changes, the test code can remain unchanged
Actual project: excel stores test data, ddt reads test data to the unit test framework (in test cases), and outputs to html report

What is data driven

It is the change of the data that drives the execution of the automated test, and finally causes the change of the test result. To put it bluntly, it is the application of parameterization

1. DDT use - pass the basic data type

# 导入ddt库下所有内容
from ddt import *


# 在测试类前必须首先声明使用 ddt
@ddt
class imoocTest(unittest.TestCase):
    
    # int
    @data(1, 2, 3, 4)
    def test_int(self, i):
        print("test_int:", i)

    # str
    @data("1", "2", "3")
    def test_str(self, str):
        print("test_str:", str)

Test Results

test_int: 1
test_int: 2
test_int: 3
test_int: 4
test_str: 1
test_str: 2
test_str: 3

If you want to use DDT, you must first add @ddt to the unit test class

2. DDT use - passing a complex data structure

from ddt import *


# 在测试类前必须首先声明使用 ddt
@ddt
class imoocTest(unittest.TestCase):
    tuples = ((1, 2, 3), (1, 2, 3))
    lists = [[1, 2, 3], [1, 2, 3]]

    # 元组
    @data((1, 2, 3), (1, 2, 3))
    def test_tuple(self, n):
        print("test_tuple", n)

    # 列表
    @data([1, 2, 3], [1, 2, 3])
    @unpack
    def test_list(self, n1, n2, n3):
        print("test_list", n1, n2, n3)

    # 元组2
    @data(*tuples)
    def test_tuples(self, n):
        print("test_tuples", n)

    # 列表2
    @data(*lists)
    @unpack
    def test_lists(self, n1, n2, n3):
        print("test_lists", n1, n2, n3)

    # 字典
    @data({
    
    'value1': 1, 'value2': 2}, {
    
    'value1': 1, 'value2': 2})
    @unpack
    def test_dict(self, value1, value2):
        print("test_dict", value1, value2)

Test Results

test_dict 1 2
test_dict 1 2
test_list 1 2 3
test_list 1 2 3
test_lists 1 2 3
test_lists 1 2 3
test_tuple (1, 2, 3)
test_tuple (1, 2, 3)
test_tuples (1, 2, 3)
test_tuples (1, 2, 3)

Note:
@unpack : used when complex data structures are passed. For example, using a tuple or list, after adding @unpack, ddt will automatically map the tuple or list to multiple parameters. Dictionaries can also be handled like this

When unpack is not added, only one parameter of the test_case method can be filled; such as the tuple example

When you add unpack, the amount of data passed needs to be consistent; for example, in the list example, I have fixedly passed three data for each list, and an error will be reported when you pass more or less, and the parameters of the test_case method should also be written Three, need to match

When the data passed is a dictionary type, it should be noted that the keys of each dictionary must be consistent, and the names of the parameters of the test_case should also be consistent; for example, the keys of the two dictionaries are value1 and value2, and the parameters of the method are also

When the data passed is through variables, such as tuple 2, list 2, you need to add * before the variable

3. DDT use - pass json file

json file

{
    
    
  "first": [
    {
    
    
      "isRememberMe": "True",
      "password": "111111",
      "username": "root"
    },
    "200"
  ],
  "second": [
    "{'isRememberMe': True, 'password': '1111111', 'username': 'root'}",
    "406"
  ],
  "third": [
    1,
    2
  ],
  "four": "123123"
}

unit test class

from ddt import *


# 在测试类前必须首先声明使用 ddt
@ddt
class imoocTest(unittest.TestCase):

    @file_data('F:/test/config/testddt.json')
    def test_json(self, data):
        print(data)

Test Results

[{
    
    'isRememberMe': 'True', 'password': '111111', 'username': 'root'}, '200']
["{'isRememberMe': True, 'password': '1111111', 'username': 'root'}", '406']
[1, 2]
123123

4. DDT use - transfer Yaml file

yaml file

unsorted_list:
  - 10
  - 15
  - 12

sorted_list: [ 15, 12, 50 ]

unit test class

from ddt import *


# 在测试类前必须首先声明使用 ddt
@ddt
class imoocTest(unittest.TestCase):

    @file_data('F:/test/config/testddt.yaml')
    def test4(self, data):
        print("yaml", data)

Test Results

yaml [10, 15, 12]
yaml [15, 12, 50]
The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Every effort will not be in vain, even if the result is not what you want, it will accumulate experience and growth for you. So don't be afraid to fail and don't stop moving forward. Only by continuing to struggle can you constantly surpass yourself and welcome a better future!

Only by working hard can you chase your dreams; only by bravely facing difficulties can you stand at the pinnacle of life; only by doing your best can you reap a brilliant life. So, don't stop the pace of progress, we must persist in striving to make the future better!

The greatest challenge in life is not encountering difficulties, but how to overcome one's own inertia and fear to meet the challenge. Anything is possible if you have determination and perseverance. Believe in yourself, move forward firmly, and you will see the dawn of success.

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/131399976