# Sometimes we want to mark a function with an attribute. # Define the decorator testthis which will set the # attribute ._run_test on the function object. That # function will then be tested. Run tests with: # python3 decorator_attribute.py # or # python2 decorator_attribute.py from __future__ import print_function def testthis(func): ... # This should be run as a test def example_1(): print('testing this') # This should be run as a test def example_2(): print('testing that') if __name__ == '__main__': print('running the tests') for name, value in dict(locals()).items(): if getattr(value, '_run_test', False): print('running', name) value() # To test run: # python3 decorator_attribute.py # or # python2 decorator_attribute.py # and check if the output contains # running example_1 # running example_2 # No automatic test, sorry! # Something like this done by # @unittest.skip, @unittest.expectedFailure, # @pytest.mark.skipif, @pytest.mark.xfail.