Python is used for data driven ddt, batch execution, modifications, ddt Code

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

  The use case is driven by data, in addition to the test data for each test case is not the same accident, all of the use cases the code is the same, for example using the batch execution, we will come to batch execution of test cases using data-driven ideas;

  benefit:

    Batch execution of test cases, improve the efficiency of execution of the test; separation of data and scripts, test data needs to be modified, does not need to modify the code, just modify the data to an Excel file;

2. The data driving action:

  Examples of a method to automatically generate a number of embodiments according to the batch execution Example

from ddt Import ddt, data 

@ddt         # ddt and data is the gold partner, to be used with the job 
class TestMulti (unittest.TestCase): 

    # get the test case in the file, * cases () were unpacking, use-case name is automatically created: + 01 test_negative_multiply 
    # * after unpacking cases, there will be four dictionaries, data corresponding to the position parameter, a position parameter to go first one_case 
    # 1. for each iteration, a different method creates instances; 01 automatically add, execute all test cases used to traverse 
    # 1. create an instance method for performing a test case, must be beginning to test_ 
    @data (* Cases)
     DEF test_negative_multiply (Self, one_case): 
        . . . . . . . . 
        . . . . . . . 
        Other code

ddt Summary:

  • Implementation of how many use cases, a consistent number (position) parameter under the number of data decorator embodiment performs, each perform a use case, automatically a parameter to one_case, when the last parameter to one_case, and use cases after the execution, the program is finished
  • ddt and data is the gold partner, to be used with the job

4. Save as copy ddt source, not the proposed changes in the source code 

  Ddt modify the source code 107 line, use cases were not clear test_negative_multiply_01 test report
  Role:

    If the data dictionary, the dictionary as a value corresponding to the title is acquired, the name of the test report to an HTML

# Modification probably this method the source 107 lines 
DEF mk_test_name (name, value, index = 0):
     "" " 
    name: Use Case Name / example method name 
    value: Use Case Data Dictionary 
    " "" 

    # the 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" the format (name, index). 

    # Add the processing of the data dictionary 
    # If the judgment is not a dictionary type, condition is not satisfied; value is not a dictionary type and false, it is not according to the original stitching 
    IF  not is_trivial (value) and  not isinstance (value, dict):
         return  "_. 1 {0} {} " .format (name, index) 

    # if the data dictionary, the dictionary corresponding to the value in the title from among the acquired added to test name 
    IF the isinstance (value, dict):
         the try :
             # removed title value 
            value value = [ " title " ]
         the except a KeyError:
             return  " {0}} _ {. 1 " .format (name, index) 

    the try : 
        value = STR (value)       # the title converted to a string 
    the except UnicodeEncodeError:
         # fallback python2 for 
        value = value.encode ( ' ASCII' , ' Backslashreplace ' )    # value is the title name 
    test_name = " {0} _ {_} {2}. 1 " .format (name, index, value)     # with Example Name splicing 
    return the re.sub (R & lt ' \ W is | ^ (? = \ d) ' , ' _ ' , test_name)

 

 

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

Guess you like

Origin www.cnblogs.com/shouhu/p/12157840.html
ddt