Metaclass for BitcoinTestFramework #12835

issue WillAyd opened this issue on March 30, 2018
  1. WillAyd commented at 7:37 AM on March 30, 2018: contributor

    I'm new to bitcoin coming from a Python background. Wrapping my head around your test suite, I noticed that the base framework test class makes comments about how subclasses should override set_test_params and run_test but should not override __init__ or main:

    https://github.com/bitcoin/bitcoin/blob/3b62a913865777426c11992bae622fb48aa72a40/test/functional/test_framework/test_framework.py#L44

    To make that more explicit, have we considered a meta class to actually check that those assumptions are valid instead of leaving it to the user to read the docstring and follow the rules?

    I've provided a sample implementation in the details below, where the commented methods would raise an error if included in any module. Happy to submit a PR if you think it is worth pursuing.

    <details>

    class BitcoinTestMetaclass(type):
    
        def __new__(cls, clsname, bases, dct):
            if not ('run_test' in dct and 'set_test_params' in dct):
                raise TypeError("BitcoinTestFramework subclasses must override "
                                "'run_test' and 'set_test_params'")
            if '__init__' in dct or 'main' in dct:
                raise TypeError("BitcoinTestFramework subclasses may not override "
                                "'__init__' or 'main'")
            return super().__new__(cls, clsname, bases, dct)
    
        
    class BitcoinTestFramework(metaclass=BitcoinTestMetaclass):
        def set_test_params(self):
            ...
    
        def run_test(self):
            ...
    
    ''' 
    class BadSubclass1(BitcoinTestFramework):
        """Doesn't subclass run_test"""
        def set_test_params(self):
            pass
    '''
    
    '''
    class BadSubclass2(BitcoinTestFramework):
        """Overrides set_test_params and run_test, but also overrides __init__"""
        def __init__(self):
            super().__init__()
    
        def set_test_params(self):
            pass
    
        def run_test(self):
            pass
    '''
    
    class GoodSubclass(BitcoinTestFramework):
        def set_test_params(self):
            pass
    
        def run_test(self):
            pass    
    
  2. fanquake added the label Tests on Mar 30, 2018
  3. jnewbery commented at 6:08 PM on March 30, 2018: member

    Very cool. Yes, please submit a PR!

  4. laanwj closed this on Apr 8, 2018

  5. laanwj referenced this in commit 27278dffe8 on Apr 8, 2018
  6. stamhe referenced this in commit fd6f881d5c on Jun 27, 2018
  7. HashUnlimited referenced this in commit 10c8e19683 on Aug 15, 2018
  8. lionello referenced this in commit b08a4d96ba on Nov 7, 2018
  9. joemphilips referenced this in commit 1dd6cfb4d9 on Nov 9, 2018
  10. UdjinM6 referenced this in commit 5f3410c9cd on May 21, 2021
  11. UdjinM6 referenced this in commit ddff680749 on May 21, 2021
  12. UdjinM6 referenced this in commit 2b63131199 on May 21, 2021
  13. UdjinM6 referenced this in commit e0eea2db3c on May 22, 2021
  14. kittywhiskers referenced this in commit 9bb1e96c2e on May 25, 2021
  15. DrahtBot locked this on Sep 8, 2021
  16. gades referenced this in commit b7178b2afc on May 12, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-29 03:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me