This PR refactors BitcoinTestFramework to encapsulate setup and shutdown logic into dedicated methods, and adds a TestWrapper TestShell child class. This wrapper allows the underlying BitcoinTestFramework to run between user inputs in a REPL environment, such as a Jupyter notebook or any interactive Python3 interpreter.
The TestWrapper TestShell is motivated by the opportunity to expose the test-framework as a prototyping and educational toolkit. Examples of code prototypes enabled by TestWrapper TestShell can be found in the Optech Taproot/Schnorr workshop repository.
Usage example:
>>> import sys
>>> sys.path.insert(0, "/path/to/bitcoin/test/functional")
>>> from test_framework.test_wrapper import TestShell
>>> test = TestShell()
>>> test.setup(num_nodes=2)
20XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Initializing test directory /path/to/bitcoin_func_test_XXXXXXX
>>> test.nodes[0].generate(101)
>>> test.nodes[0].getblockchaininfo()["blocks"]
101
>>> test.shutdown()
20XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Stopping nodes
20XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Cleaning up /path/to/bitcoin_func_test_XXXXXXX on exit
20XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Tests successful
Overview of changes to BitcoinTestFramework:
- Code moved to
setup()/shutdown()methods. - Argument parsing logic encapsulated by
parse_argsmethod. - Success state moved to
BitcoinTestFramework.success.
During Shutdown
BitcoinTestFrameworklogging handlers are flushed and removed.BitcoinTestFrameowork.nodeslist is cleared.NetworkThread.network_event_loopis reset. (NetworkThread class).
Behavioural changes:
- Test parameters can now also be set when overriding BitcoinTestFramework.setup() in addition to overriding
set_test_paramsmethod. - Potential exceptions raised in BitcoinTestFramework.setup() will be handled in main().
Added files:
test_wrapper.pytest_shell.pytest-wrapper.mdtest-shell.md