Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does __test__ = False magic attribute work for test discovery

Tags:

python

So I'm attempting to implement something similar to how unittesting frameworks do the following thing:

class BaseTest(T.TestCase):
    # Disables this test from being run
    __test__ = False

    def test_foo(self): pass


# However this test is picked up because it doesn't directly have __test__ set
class InheritingTest(BaseTest): pass

A thing I find peculiar:

# >> InheritingTest.__test__
# False

Which would indicate to me that it isn't using a metaclass to set __test__ to True on construction of the type.

I tried grepping through the python library find . -name "*.py" | xargs grep '__test__' but did not seem to find anything related to this.

My "guess" approach at solving this problem is to do the following:

def is_class_tested(cls):
    return cls.__dict__.get('__test__', True)

However this feels fragile to me... Is there a cleaner / nicer way to do this that works in all cases? Is there ever a chance that a class will not have a __dict__ property?

like image 623
Anthony Sottile Avatar asked Aug 25 '13 23:08

Anthony Sottile


1 Answers

Testify, the library you are using, does the following in testify/test_discovery.py around line 140:

    # it's not a list, it's not a bare module - let's see if it's an honest-to-god TestCaseBase
    elif isinstance(test_module, MetaTestCase) and (not '__test__' in test_module.__dict__ or bool(test_module.__test__)):
        ...
    # detect unittest test cases
    elif issubclass(test_module, unittest.TestCase) and (not '__test__' in test_module.__dict__ or bool(test_module.__test__)):

So, in other words, it is doing exactly what your "guess" approach does, in a slightly more verbose way: it tests for the presence and value of __test__ in the class __dict__ directly.

like image 117
nneonneo Avatar answered Sep 28 '22 17:09

nneonneo