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


Groups > comp.lang.python > #43263

Re: name lookup failure using metaclasses with unittests

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; 'attribute': 0.07; 'context': 0.07; 'detect': 0.07; 'error:': 0.07; 'problem:': 0.07; 'report.': 0.07; 'upgraded': 0.07; '__init__': 0.09; '__name__': 0.09; 'bug.': 0.09; 'derived': 0.09; 'exec': 0.09; 'module)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'tmp': 0.09; 'unittest': 0.09; 'valueerror:': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'windows': 0.15; '"python': 0.16; '"test"': 0.16; '2.7.3': 0.16; 'bases,': 0.16; 'discovery.': 0.16; 'eckhardt': 0.16; 'fname,': 0.16; 'invocation': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'overriding': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'test".': 0.16; 'attach': 0.16; 'wrote:': 0.18; 'library': 0.18; 'module': 0.19; 'trying': 0.19; 'import': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'recognize': 0.24; 'simpler': 0.24; 'specify': 0.24; 'looks': 0.24; 'environment': 0.24; 'question': 0.24; 'equivalent': 0.26; 'skip:" 30': 0.26; 'skip:" 40': 0.26; 'pass': 0.26; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'correct': 0.29; "i'm": 0.30; 'asked': 0.31; 'code': 0.31; 'relies': 0.31; 'suites': 0.31; 'file': 0.32; 'class': 0.32; 'extend': 0.32; 'run': 0.32; '(most': 0.33; 'guess': 0.33; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'classes': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'case,': 0.35; 'test': 0.35; 'but': 0.35; 'functions.': 0.36; 'words,': 0.36; 'doing': 0.36; 'method': 0.36; 'should': 0.36; 'reports': 0.37; 'wrong': 0.37; 'being': 0.38; 'problems': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; '12,': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'name': 0.63; 'such': 0.63; 'therefore,': 0.64; 'provide': 0.64; 'here': 0.66; 'below.': 0.71; 'discovered': 0.83; '128,': 0.84; 'all!': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: name lookup failure using metaclasses with unittests
Date Wed, 10 Apr 2013 11:52:56 +0200
Organization None
References <sqjf3a-03s.ln1@satorlaser.homedns.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b041.dip.t-dialin.net
User-Agent KNode/4.7.3
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.404.1365587492.3114.python-list@python.org> (permalink)
Lines 92
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365587492 news.xs4all.nl 2603 [2001:888:2000:d::a6]:40954
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43263

Show key headers only | View raw


Ulrich Eckhardt wrote:

> Hi!
> 
> I'm having problems using a metaclass to generate test functions. This
> works when I try to run all tests from the module or test case, but it
> fails when I'm trying to specify a single test function. My environment
> is Python 2.7.3 on MS Windows 7 at the moment. It should be upgraded to
> at least 2.7.4 or better to 3, but see the notes on Python 3 below.
> 
> # my_module.py
> import unittest
> class X(unittest.TestCase):
>      def __metaclass__(name, bases, dict):
>          # attach function
>          def test(self):
>              pass
>          dict['test_1'] = test
>          dict['test_2'] = test
>          # create class
>          return type(name, bases, dict)
> 
> The error when I'm trying to run "python -m unittest my_module.X.test_1"
> is: "Value error: no such test method in <class 'my_module.X'>: test".
> The astonishing part is that it claims that "test" is not found while I
> asked it to run "test_1". The name it complains about is the name of the
> function inside the metaclass function. In all other cases, like e.g.
> giving "-v" it reports the correct function name. My question here is
> whether I'm doing something wrong or whether I discovered a bug.

Here's a simpler demo of the problem:

$ cat tmp.py
import unittest

class X(unittest.TestCase):
    def test_1(self): pass
    test_1.__name__ = "test_2"

$ python -m unittest -v tmp
test_1 (tmp.X) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
$ python -m unittest -v tmp.X.test_1
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/lib/python2.7/unittest/__main__.py", line 12, in <module>
    main(module=None)
  File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs
    self.createTests()
  File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests
    self.module)
  File "/usr/lib/python2.7/unittest/loader.py", line 128, in 
loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib/python2.7/unittest/loader.py", line 109, in 
loadTestsFromName
    return self.suiteClass([parent(obj.__name__)])
  File "/usr/lib/python2.7/unittest/case.py", line 191, in __init__
    (self.__class__, methodName))
ValueError: no such test method in <class 'tmp.X'>: test_2

It looks like this particular invocation relies on class attribute and 
function __name__ being identical.

Please file a bug report.

> Now, concerning Python 3, it fails to detect any test case at all! My
> guess is that the unittest library was changed to use metaclasses itself
> in order to detect classes derived from unittest.TestCase. Therefore,
> overriding the metaclass breaks test case discovery. My question in that
> context is how do I extend metaclasses instead of overriding it? In
> other words, what is the equivalent to super() for class creation?

Python 3 does not recognize the __metaclass__ attribute as the metaclass. 
You need to provide it like so:

def __metaclass__(name, bases, dict):
    ...

class X(unittest.TestCase, metaclass=__metaclass__):
    pass

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-10 10:18 +0200
  Re: name lookup failure using metaclasses with unittests Peter Otten <__peter__@web.de> - 2013-04-10 11:52 +0200
    Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-11 09:09 +0200
    Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-11 08:43 +0200
      Re: name lookup failure using metaclasses with unittests Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-11 08:19 +0000
        Re: name lookup failure using metaclasses with unittests Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-04-12 09:17 +0200
          Re: name lookup failure using metaclasses with unittests Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-12 10:58 -0400
      Re: name lookup failure using metaclasses with unittests Arnaud Delobelle <arnodel@gmail.com> - 2013-04-11 09:19 +0100

csiph-web