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


Groups > comp.lang.python > #43259

name lookup failure using metaclasses with unittests

From Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Newsgroups comp.lang.python
Subject name lookup failure using metaclasses with unittests
Date 2013-04-10 10:18 +0200
Message-ID <sqjf3a-03s.ln1@satorlaser.homedns.org> (permalink)

Show all headers | View raw


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.


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?

Thank you for your help!

Uli

Back to comp.lang.python | Previous | NextNext 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