A test method parameterization class decorator. Parameters are specified as the value of a class attribute that ends with the string '_params'. Call the portion before '_params' the prefix. Then a method to be parameterized must have the same prefix, the string '_as_', and an arbi
(cls)
| 73 | |
| 74 | |
| 75 | def parameterize(cls): |
| 76 | """A test method parameterization class decorator. |
| 77 | |
| 78 | Parameters are specified as the value of a class attribute that ends with |
| 79 | the string '_params'. Call the portion before '_params' the prefix. Then |
| 80 | a method to be parameterized must have the same prefix, the string |
| 81 | '_as_', and an arbitrary suffix. |
| 82 | |
| 83 | The value of the _params attribute may be either a dictionary or a list. |
| 84 | The values in the dictionary and the elements of the list may either be |
| 85 | single values, or a list. If single values, they are turned into single |
| 86 | element tuples. However derived, the resulting sequence is passed via |
| 87 | *args to the parameterized test function. |
| 88 | |
| 89 | In a _params dictionary, the keys become part of the name of the generated |
| 90 | tests. In a _params list, the values in the list are converted into a |
| 91 | string by joining the string values of the elements of the tuple by '_' and |
| 92 | converting any blanks into '_'s, and this become part of the name. |
| 93 | The full name of a generated test is a 'test_' prefix, the portion of the |
| 94 | test function name after the '_as_' separator, plus an '_', plus the name |
| 95 | derived as explained above. |
| 96 | |
| 97 | For example, if we have: |
| 98 | |
| 99 | count_params = range(2) |
| 100 | |
| 101 | def count_as_foo_arg(self, foo): |
| 102 | self.assertEqual(foo+1, myfunc(foo)) |
| 103 | |
| 104 | we will get parameterized test methods named: |
| 105 | test_foo_arg_0 |
| 106 | test_foo_arg_1 |
| 107 | test_foo_arg_2 |
| 108 | |
| 109 | Or we could have: |
| 110 | |
| 111 | example_params = {'foo': ('bar', 1), 'bing': ('bang', 2)} |
| 112 | |
| 113 | def example_as_myfunc_input(self, name, count): |
| 114 | self.assertEqual(name+str(count), myfunc(name, count)) |
| 115 | |
| 116 | and get: |
| 117 | test_myfunc_input_foo |
| 118 | test_myfunc_input_bing |
| 119 | |
| 120 | Note: if and only if the generated test name is a valid identifier can it |
| 121 | be used to select the test individually from the unittest command line. |
| 122 | |
| 123 | The values in the params dict can be a single value, a tuple, or a |
| 124 | dict. If a single value of a tuple, it is passed to the test function |
| 125 | as positional arguments. If a dict, it is a passed via **kw. |
| 126 | |
| 127 | """ |
| 128 | paramdicts = {} |
| 129 | testers = collections.defaultdict(list) |
| 130 | for name, attr in cls.__dict__.items(): |
| 131 | if name.endswith('_params'): |
| 132 | if not hasattr(attr, 'keys'): |
nothing calls this directly
no test coverage detected
searching dependent graphs…