Skip to content

Commit 27278df

Browse files
committed
Merge #12856: Tests: Add Metaclass for BitcoinTestFramework
c9cce0a Tests: Add Metaclass for BitcoinTestFramework (Will Ayd) Pull request description: BitcoinTestFramework instructs developers in its docstring to override `set_test_params` and `run_test` in subclasses while being sure NOT to override `__init__` and `main` . This change adds a metaclass to ensure that developers adhere to that protocol, raising a ``TypeError`` in instances where they have not. closes #12835 Tree-SHA512: 5a47a7ead1f18361138cad4374747c4a8f29d25506f7b2c2a8c1c966a0b65e5ccf7317f9a078df8680fdab5d3fb71fee46a159c9f381878a3683c1e9f874abbe
2 parents bd42b85 + c9cce0a commit 27278df

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

test/functional/test_framework/test_framework.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,28 @@ class TestStatus(Enum):
4141
TEST_EXIT_FAILED = 1
4242
TEST_EXIT_SKIPPED = 77
4343

44-
class BitcoinTestFramework():
44+
45+
class BitcoinTestMetaClass(type):
46+
"""Metaclass for BitcoinTestFramework.
47+
48+
Ensures that any attempt to register a subclass of `BitcoinTestFramework`
49+
adheres to a standard whereby the subclass overrides `set_test_params` and
50+
`run_test` but DOES NOT override either `__init__` or `main`. If any of
51+
those standards are violated, a ``TypeError`` is raised."""
52+
53+
def __new__(cls, clsname, bases, dct):
54+
if not clsname == 'BitcoinTestFramework':
55+
if not ('run_test' in dct and 'set_test_params' in dct):
56+
raise TypeError("BitcoinTestFramework subclasses must override "
57+
"'run_test' and 'set_test_params'")
58+
if '__init__' in dct or 'main' in dct:
59+
raise TypeError("BitcoinTestFramework subclasses may not override "
60+
"'__init__' or 'main'")
61+
62+
return super().__new__(cls, clsname, bases, dct)
63+
64+
65+
class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
4566
"""Base class for a bitcoin test script.
4667
4768
Individual bitcoin test scripts should subclass this class and override the set_test_params() and run_test() methods.
@@ -434,6 +455,7 @@ def _initialize_chain_clean(self):
434455
for i in range(self.num_nodes):
435456
initialize_datadir(self.options.tmpdir, i)
436457

458+
437459
class SkipTest(Exception):
438460
"""This exception is raised to skip a test"""
439461
def __init__(self, message):

0 commit comments

Comments
 (0)