Skip to content

Commit c9cce0a

Browse files
committed
Tests: Add Metaclass for BitcoinTestFramework
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
1 parent 9a2db3b commit c9cce0a

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.
@@ -432,6 +453,7 @@ def _initialize_chain_clean(self):
432453
for i in range(self.num_nodes):
433454
initialize_datadir(self.options.tmpdir, i)
434455

456+
435457
class SkipTest(Exception):
436458
"""This exception is raised to skip a test"""
437459
def __init__(self, message):

0 commit comments

Comments
 (0)