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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to