@ddt.ddt (class decorator, declares that the current class uses the ddt framework)
@ ddt.data (function decorator, used to pass data to test cases), supports passing all python data types: numbers (int, long, float, compix), strings, lists, tuples, sets, writing and reading data File function, @data entry parameter plus * to read
@ddt.unpack (write to the decorator to unpack the transmitted data packet), generally acts on tuples and tuples List, dictionary (the name and number of parameters need to be consistent with the keys of the dictionary) (not required for arrays and strings)
@ddt.file_data (function decorator, can be read directly Take yaml/json file)
Data-Driven Tests (DDT) is data-driven testing, which can implement different data Run the same test case. The essence of ddt is actually a decorator, a set of data and a scene.
Keyword driven (core: encapsulate business logic into keyword login, only need to call login.)
(1) Single parameter: guide package - write a parameter (list, number, string) -----Set the @ddt.data decorator to write the parameter name----Method Write the formal parameter *data----call parameter content
(2) Multi-parameter data-driven test (one test parameter contains multiple elements): Guide package-set @ddt decoration Device - set @unpack unpacking - write parameters - formal parameter transfer - call
(3) txt file parameter transfer
(4 ) json file parameter passing
(5) yaml file parameter passing
(6) xlsx file parameter passing
Note: variable parameters are passed in Python: * represents sequential reading List type, ** represents the type of sequential reading object (dictionary), click to read the variable parameter part to learn about the related mechanism
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
Examples are as follows:
Method 1: The test data is written directly in list form, Use ddt.data(*Data) to pass the value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Method 2: Write data to the method form readData(), use ddt.data(*readData()) to pass the value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
The above is the detailed content of How to implement Python Unittest ddt data driver. For more information, please follow other related articles on the PHP Chinese website!