| 1 |
def parameterize_cases(argnames, cases): |
| 2 |
""" |
| 3 |
This is used for parametrizing pytest test cases. |
| 4 |
|
| 5 |
argnames: a comma separated string of arguments that the test expects. |
| 6 |
cases: a dictionary of test cases. |
| 7 |
|
| 8 |
argnames_list = [i.strip() for i in argnames.split(',')] |
| 9 |
ids = list(cases.keys()) |
| 10 |
argvalues = [tuple(test_name if k == 'test_name' else i[k - 1] for k in argnames_list) for test_name, i in cases.items()] |
| 11 |
return {'argnames': argnames, 'argvalues': argvalues, 'ids': ids} |
| 12 |
|
| 13 |
|
| 14 |
""" |
| 15 |
argnames_list = [i.strip() for i in argnames.split(',')] |
| 16 |
if 'test_name' not in argnames_list: |
| 17 |
argnames_list.append('test_name') |
| 18 |
argvalues = [tuple(test_name if (k == 'test_name') else test_dict[k] for k in argnames_list) for test_name, test_dict in cases.items()] |
| 19 |
ids = list(cases.keys()) |
| 20 |
return {'argnames': argnames, 'argvalues': argvalues, 'ids': ids} |
| 21 |
|
| 22 |
|
| 23 |
class CustomClass: |
| 24 |
def __init__(self, a, b=None): |
| 25 |
self.a = a |
| 26 |
self.b = b |
| 27 |
|
| 28 |
def __str__(self): |
| 29 |
return "Custom({}, {})".format(self.a, self.b) |
| 30 |
|
| 31 |
def __repr__(self): |
| 32 |
return self.__str__() |
| 33 |
|
| 34 |
|
| 35 |
class CustomClassMisleadingRepr(CustomClass): |
| 36 |
def __str__(self): |
| 37 |
return "({}, {})".format(self.a, self.b) |
| 38 |
|
| 39 |
|
| 40 |
class CustomClass2: |
| 41 |
def __init__(self, prop1=None, prop2=None): |
| 42 |
self.prop1 = prop1 or [] |
| 43 |
self.prop2 = prop2 or [] |
| 44 |
|
| 45 |
def __eq__(self, other): |
| 46 |
return self.__dict__ == other.__dict__ |
| 47 |
|
| 48 |
def __repr__(self): |
| 49 |
return "<CustomClass2 id: {}, prop1: {}, prop2: {}>".format( |
| 50 |
id(self), self.prop1, self.prop2) |
| 51 |
|
| 52 |
__str__ = __repr__ |
| 53 |
|
| 54 |
|
| 55 |
class PicklableClass: |
| 56 |
def __init__(self, item): |
| 57 |
if item != 'delete': |
| 58 |
self.item = item |
| 59 |
|
| 60 |
def __reduce__(self): |
| 61 |
if hasattr(self, 'item'): |
| 62 |
item = self.item |
| 63 |
else: |
| 64 |
item = 'delete' |
| 65 |
return (self.__class__, (item, )) |
| 66 |
|
| 67 |
def __eq__(self, other): |
| 68 |
if hasattr(self, 'item') and hasattr(other, 'item'): |
| 69 |
return self.item == other.item |
| 70 |
if not hasattr(self, 'item') and not hasattr(other, 'item'): |
| 71 |
return True |
| 72 |
return False |
| 73 |
|
| 74 |
def __str__(self): |
| 75 |
return f"<Picklable: {self.item if hasattr(self, 'item') else 'delete'}>" |
| 76 |
|
| 77 |
__repr__ = __str__ |