In Python, ddt is used to drive data, execute use cases in batches, and modify ddt code

1. What is data-driven? What are the benefits of using data-driven?

Use case execution is driven by data. Every test case has the same code except for different test data. In order to use case execution in batch, we will use the idea of data-driven to execute test cases in batch;

Benefits:

Batch execution of test cases improves the efficiency of test execution; data and script are separated, when test data needs to be modified, the code does not need to be modified, only the data of Excel file needs to be modified;

2. Data driven function:

Automatically generate instance methods according to the number of use cases, and execute use cases in batches

from ddt import ddt, data

@ddt        # ddt and data It's the golden partner, Use it together
class TestMulti(unittest.TestCase):

    # Get the test case in the file,*cases()Unpack and automatically create case name: test_negative_multiply + 01
    # *cases After unpacking, there will be four dictionaries, equivalent to data First go to the first position parameter to one_case
    # 1. Each iteration will create a different instance method; add 01 automatically to traverse and execute all test cases
    # 1. Create instance method, Used to execute a test case,Be sure to test_Start
    @data(*cases)
    def test_negative_multiply(self, one_case):
        . . . . . . . . 
        . . . . . . . 
        //Other code

ddt concludes:

  • How many use cases have been executed? The number of cases executed is the same as the number of (location) parameters of the data decorator. Each case executed will automatically pass a parameter to one case. When the last parameter is passed to one case and the execution of the use case is finished, the program will be finished
  • ddt and data are golden partners. They need to be used together

4. Copy the ddt source code and save it as. It is not recommended to modify it on the source code

Modify line 107 of ddt source code, and the case name in the test report is ambiguous
Function:

If the data is a dictionary, get the value corresponding to the title in the dictionary and add it to the HTML test report name

# Modify the method in line 107 of the source code
def mk_test_name(name, value, index=0):
    """
    name:Use case name/Instance method name
    value: Use case data dictionary
    """

    # Add zeros before index to keep order
    index = "{0:0{1}}".format(index + 1, index_len)

    # Note the following two lines
    # if not is_trivial(value):
    #     return "{0}_{1}".format(name, index)

    # Add processing of dictionary data
    # Judge if it is not a dictionary type, the condition is not satisfied; value If it is not a dictionary type, it will not be spliced as it was
    if not is_trivial(value) and not isinstance(value, dict):
        return "{0}_{1}".format(name, index)

    # If the data is a dictionary, get the title The corresponding value, added to the test case name
    if isinstance(value, dict):
        try:
            # take out title value
            value = value["title"]
        except KeyError:
            return "{0}_{1}".format(name, index)

    try:
        value = str(value)      # hold title Convert to string type
    except UnicodeEncodeError:
        # fallback for python2
        value = value.encode('ascii', 'backslashreplace')   # value yes title Name
    test_name = "{0}_{1}_{2}".format(name, index, value)    # Case name splicing
    return re.sub(r'\W|^(?=\d)', '_', test_name)

 

 

*******Please respect the original, if you want to reprint, please indicate the source: reprint from: https://www.cnblogs.com/shouhu/ Thank you!! * * * * * *

Keywords: Python Excel ascii

Added by reecec on Tue, 07 Jan 2020 00:57:50 +0200