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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With