Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52175
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" |
| Date | 2013-08-08 04:04 -0400 |
| References | <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.341.1375949083.1251.python-list@python.org> (permalink) |
On 8/8/2013 2:32 AM, adam.preble@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-07 23:32 -0700
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Terry Reedy <tjreedy@udel.edu> - 2013-08-08 04:04 -0400
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-08 09:20 -0700
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Terry Reedy <tjreedy@udel.edu> - 2013-08-08 15:28 -0400
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Peter Otten <__peter__@web.de> - 2013-08-08 10:32 +0200
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Peter Otten <__peter__@web.de> - 2013-08-08 10:50 +0200
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-08 09:17 -0700
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Ned Batchelder <ned@nedbatchelder.com> - 2013-08-08 13:14 -0400
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-08 22:07 -0700
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Peter Otten <__peter__@web.de> - 2013-08-09 08:31 +0200
Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-11 21:25 -0700
csiph-web