What I'm trying to do is to perform hundred of unit tests to a function, which I can derive from a dictionary. Unfortunately I cannot use any of the existing packages for parametrized tests (like nose), so I'm trying to get my own solution.
My intention with the following example code is to create 3 classes (one for each test) that will exist in the global scope so that unittests can pick it up and run the corresponding tests.
tests = [
{'text': 'text1fdskla3fsda4',
'result': [1, 3, 4],
},
{'text': 'fdsg45tg5b',
'result': [4, 5, 5,5 ],
},
{'text': 'fsddf4',
'result': [4, 2],
}
]
def evaluate(text):
out = []
for char in text:
if char.isdigit():
out.append(int(char))
return out
class TestMeta(type):
def __new__(cls, name, bases, attrs):
name = str(test['text'])
return type.__new__(cls, name, (unittest.TestCase,), attrs)
for test in tests:
class T(object):
__metaclass__ = TestMeta
def testOne(self):
self.assertEqual(test['result'], evaluate(test['text']))
globals()[(test['text'])] = copy.deepcopy(T)
unittest.main()
When I run the above code, I get four unittests, which is one more than expected, but on top of that, instead of having the output of each unittest, it seems like the class I'm creating is always the same (even if I'm actually setting a different name and parameters to each):
======================================================================
FAIL: testOne (__main__.fsddf4)
----------------------------------------------------------------------
Traceback (most recent call last):
File "ble.py", line 45, in testOne
self.assertEqual(test['result'], evaluate(test['text']))
AssertionError: [4, 2] != [4]
======================================================================
FAIL: testOne (__main__.fdsg45tg5b)
----------------------------------------------------------------------
Traceback (most recent call last):
File "ble.py", line 45, in testOne
self.assertEqual(test['result'], evaluate(test['text']))
AssertionError: [4, 2] != [4]
======================================================================
FAIL: testOne (__main__.fsddf4)
----------------------------------------------------------------------
Traceback (most recent call last):
File "ble.py", line 45, in testOne
self.assertEqual(test['result'], evaluate(test['text']))
AssertionError: [4, 2] != [4]
======================================================================
FAIL: testOne (__main__.text1fdskla3fsda4)
----------------------------------------------------------------------
Traceback (most recent call last):
File "ble.py", line 45, in testOne
self.assertEqual(test['result'], evaluate(test['text']))
AssertionError: [4, 2] != [4]
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=4)
deepcopy is an attempt to get a different output, but didn't help. The explicit creation of the class in the globals dict was due to the fact that simply creating the classes on the for loop just yields one unittest.
Aucun commentaire:
Enregistrer un commentaire