Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52175

Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?"

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!newsfeed.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!85.12.40.139.MISMATCH!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'tests.': 0.07; '__name__': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'true)': 0.09; 'unittest': 0.09; 'def': 0.12; 'jan': 0.12; '"test"': 0.16; 'anyhow,': 0.16; 'backgrounds': 0.16; "function's": 0.16; 'guessing': 0.16; 'instead:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:make': 0.16; 'subject:possible': 0.16; 'wrote:': 0.18; 'module': 0.19; "python's": 0.19; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "i'm": 0.30; 'work.': 0.31; "skip:' 10": 0.31; 'discovery': 0.31; 'class': 0.32; 'maybe': 0.34; 'subject:from': 0.34; 'something': 0.35; 'definition': 0.35; 'test': 0.35; 'but': 0.35; 'skip:f 40': 0.36; 'yield': 0.36; 'should': 0.36; 'changing': 0.37; 'convention': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'identify': 0.61; 'received:173': 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'believe': 0.68; 'hoping': 0.75; 'decorate': 0.84; 'received:fios.verizon.net': 0.84; 'trick,': 0.84; 'do:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
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 Thu, 08 Aug 2013 04:04:30 -0400
References <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
In-Reply-To <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.341.1375949083.1251.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375949083 news.xs4all.nl 15972 [2001:888:2000:d::a6]:45429
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52175

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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