There is a test wrapper, it internally uses .assertEquals , which compares two values. There are N tests that use this wrapper.
def example(actual, expected): message = 'not equal!!!111' self.assertEquals(actual, expected, message) example(1, 2) I want to expand the functionality so that it can be checked assertIn , but for existing tests the behavior has not changed. I.e:
- Transfer tuples, lists and dictionaries as is
- Strings and numbers wrap in an iterable (probably a tuple) of one element. Yes, the string is iterable, but I want to keep the old behavior for strings.
- If I want to pass an iterable as a single element, I will wrap it myself in a tuple
example(1, 2) # fail example(1, [1, 2]) #pass example(1, ([1, 2],)) # fail example([1, 2], ([1, 2],)) # pass example('a', 'abc') # fail example('a', ('a', 'b', 'c')) # pass example('a', list('abc')) # pass So far I have come up with a variant through hasattr :
def example(actual, expected): message = 'not equal!!!111' if not hasattr(expected, '__iter__'): expected = expected, self.assertIn(actual, expected, message) Either way:
def example(actual, expected): message = 'not equal!!!111' if hasattr(expected, '__iter__'): self.assertIn(actual, expected, message) else: self.assertEqual(actual, expected, message) Are there any weak points in this solution? How to make better / shorter? Any magic type conversion like !! in js ?