nos, unittest.TestCase i metaclass: automatycznie wygenerowany test * metody nie odkryte

Jest to kolejne pytanie dla unittest i metaclass: automatyczne generowanie test_ * metody :

Dla tej (stałej) jednostki.Układ TestCase:

#!/usr/bin/env python

import unittest


class TestMaker(type):

    def __new__(cls, name, bases, attrs):
        callables = dict([
            (meth_name, meth) for (meth_name, meth) in attrs.items() if
            meth_name.startswith('_test')
        ])

        for meth_name, meth in callables.items():
            assert callable(meth)
            _, _, testname = meth_name.partition('_test')

            # inject methods: test{testname}_v4,6(self)
            for suffix, arg in (('_false', False), ('_true', True)):
                testable_name = 'test{0}{1}'.format(testname, suffix)
                testable = lambda self, func=meth, arg=arg: func(self, arg)
                attrs[testable_name] = testable

        return type.__new__(cls, name, bases, attrs)


class TestCase(unittest.TestCase):

    __metaclass__ = TestMaker

    def test_normal(self):
        print 'Hello from ' + self.id()

    def _test_this(self, arg):
        print '[{0}] this: {1}'.format(self.id(), str(arg))

    def _test_that(self, arg):
        print '[{0}] that: {1}'.format(self.id(), str(arg))


if __name__ == '__main__':
    unittest.main()

Działa to przy użyciu frameworka stdlib. Oczekiwana i rzeczywista wydajność:

C:\Users\santa4nt\Desktop>C:\Python27\python.exe test_meta.py
Hello from __main__.TestCase.test_normal
.[__main__.TestCase.test_that_false] that: False
.[__main__.TestCase.test_that_true] that: True
.[__main__.TestCase.test_this_false] this: False
.[__main__.TestCase.test_this_true] this: True
.
----------------------------------------------------------------------
Ran 5 tests in 0.015s

OK

Jednak, ponieważ faktycznie używam nos , Ta sztuczka wydaje się z nią nie zgadzać. Wyjście jakie mam to:

C:\Users\santa4nt\Desktop>C:\Python27\python.exe C:\Python27\Scripts\nosetests test_meta.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Krótko mówiąc, metody test_* generowane przez metaklasy nie rejestrują się za pomocą nose . Can ktoś rzucił na to światło?

Dzięki,

Author: Community, 2011-03-03

1 answers

Tak więc, po sprawdzeniu zarówno kodu źródłowego stdlib unittest, jak i kodu źródłowego loadera i selektora nose ' a, okazuje się, że nose nadpisuje unittest.TestLoader.getTestCaseNames, aby użyć własnego selektora (z punktami wtyczki).

Teraz selektor nose szuka potencjalnej metody method.__name__, aby dopasować pewne wyrażenia regularne, czarno-białe listy i decyzje wtyczek.

W moim przypadku dynamicznie generowane funkcje mają swoje testable.__name__ == '<lambda>', nie pasujące do żadnego z kryteriów selektora.

Aby naprawić,

        # inject methods: test{testname}_v4,6(self)
        for suffix, arg in (('_false', False), ('_true', True)):
            testable_name = 'test{0}{1}'.format(testname, suffix)
            testable = lambda self, arg=arg: meth(self, arg)
            testable.__name__ = testable_name    # XXX: the fix
            attrs[testable_name] = testable

I pewnie wystarczy:

(sandbox-2.7)bash-3.2$ nosetests -vv 
test_normal (test_testgen.TestCase) ... ok
test_that_false (test_testgen.TestCase) ... ok
test_that_true (test_testgen.TestCase) ... ok
test_this_false (test_testgen.TestCase) ... ok
test_this_true (test_testgen.TestCase) ... ok

----------------------------------------------------------------------
Ran 5 tests in 0.005s

OK
 17
Author: Santa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-03 07:17:13