On 8/8/2013 2:32 AM, adam.pre...@gmail.com wrote:
We were coming into Python's unittest module from backgrounds in nunit, where they use a
decorate to identify tests. So I was hoping to avoid the convention of prepending
"test" to the TestClass methods that are to be actually run. I'm sure this
comes up all the time, but I mean not to have to do:
class Test(unittest.TestCase):
def testBlablabla(self):
self.assertEqual(True, True)
But instead:
class Test(unittest.TestCase):
@test
def Blablabla(self):
self.assertEqual(True, True)
I cannot help but note that this is *more* typing. But anyhow, something
like this might work.
def test(f):
f.__class__.__dict__['test_'+f.__name__]
might work. Or maybe for the body just
setattr(f.__class__, 'test_'+f.__name__)
Superficially, you'd think changing a function's __name__ should do the trick,
but it looks like test discovery happens without looking at the transformed
function.
I am guessing that unittest discovery for each class is something like
if isinstance (cls, unittest.TestCase):
for name, f in cls.__dict__.items():
if name.startswith('test'):
yield f
You were thinking it would be
...
for f in cls.__dict__.values():
if f.__name__.startwith('test'):
yield f
Not ridiculous, but you seem to have disproven it. I believe you can
take 'name' in the docs to be bound or namespace name rather than
definition or attribute name.
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list