Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1Test Shell for Interactive Environments2=========================================4This document describes how to use the `TestShell` submodule in the functional5test suite.7The `TestShell` submodule extends the `BitcoinTestFramework` functionality to8external interactive environments for prototyping and educational purposes. Just9like `BitcoinTestFramework`, the `TestShell` allows the user to:11* Manage regtest bitcoind subprocesses.12* Access RPC interfaces of the underlying bitcoind instances.13* Log events to the functional test logging utility.15The `TestShell` can be useful in interactive environments where it is necessary16to extend the object lifetime of the underlying `BitcoinTestFramework` between17user inputs. Such environments include the Python3 command line interpreter or18[Jupyter](https://jupyter.org/) notebooks running a Python3 kernel.20## 1. Requirements22* Python323* `bitcoind` built in the same repository as the `TestShell`.25## 2. Importing `TestShell` from the Bitcoin Core repository27We can import the `TestShell` by adding the path of the configured Bitcoin Core28`test_framework` module to the beginning of the PATH variable, and then29importing the `TestShell` class from the `test_shell` sub-package. Since30the build system creates a copy of the `test_framework` module into a new `build/`31directory along with the required configuration file, the path to the build copy32must be used.34```35>>> import sys36>>> sys.path.insert(0, "/path/to/bitcoin/build/test/functional")37>>> from test_framework.test_shell import TestShell38```40The following `TestShell` methods manage the lifetime of the underlying bitcoind41processes and logging utilities.43* `TestShell().setup()`44* `TestShell().shutdown()`46The `TestShell` inherits all `BitcoinTestFramework` members and methods, such47as:48* `TestShell().nodes[index].rpc_method()`49* `TestShell().log.info("Custom log message")`51The following sections demonstrate how to initialize, run, and shut down a52`TestShell` object.54## 3. Initializing a `TestShell` object56```57>>> test = TestShell().setup(num_nodes=2, setup_clean_chain=True)5820XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Initializing test directory /path/to/bitcoin_func_test_XXXXXXX59```60The `TestShell` forwards all functional test parameters of the parent61`BitcoinTestFramework` object. The full set of argument keywords which can be62used to initialize the `TestShell` can be found in [section63#6](#custom-testshell-parameters) of this document.65**Note: Running multiple instances of `TestShell` is not allowed.** Running a66single process also ensures that logging remains consolidated in the same67temporary folder. If you need more bitcoind nodes than set by default (1),68simply increase the `num_nodes` parameter during setup.70```71>>> test2 = TestShell().setup()72TestShell is already running!73```75## 4. Interacting with the `TestShell`77Unlike the `BitcoinTestFramework` class, the `TestShell` keeps the underlying78Bitcoind subprocesses (nodes) and logging utilities running until the user79explicitly shuts down the `TestShell` object.81During the time between the `setup` and `shutdown` calls, all `bitcoind` node82processes and `BitcoinTestFramework` convenience methods can be accessed83interactively.85**Example: Mining a regtest chain**87By default, the `TestShell` nodes are initialized with a clean chain. This means88that each node of the `TestShell` is initialized with a block height of 0.90```91>>> test.nodes[0].getblockchaininfo()["blocks"]92093```95We now let the first node generate 101 regtest blocks, and direct the coinbase96rewards to a wallet address owned by the mining node.98```99>>> test.nodes[0].createwallet('default')100{'name': 'default'}101>>> address = test.nodes[0].getnewaddress()102>>> test.generatetoaddress(test.nodes[0], 101, address)103['2b98dd0044aae6f1cca7f88a0acf366a4bfe053c7f7b00da3c0d115f03d67efb', ...104```105Since the two nodes are both initialized by default to establish an outbound106connection to each other during `setup`, the second node's chain will include107the mined blocks as soon as they propagate.109```110>>> test.nodes[1].getblockchaininfo()["blocks"]111101112```113The block rewards from the first block are now spendable by the wallet of the114first node.116```117>>> test.nodes[0].getbalance()118Decimal('50.00000000')119```121We can also log custom events to the logger.123```124>>> test.nodes[0].log.info("Successfully mined regtest chain!")12520XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework.node0 (INFO): Successfully mined regtest chain!126```128**Note: Please also consider the functional test129[readme](/test/functional/README.md), which provides an overview of the130test-framework**. Modules such as131[key.py](/test/functional/test_framework/key.py),132[script.py](/test/functional/test_framework/script.py) and133[messages.py](/test/functional/test_framework/messages.py) are particularly134useful in constructing objects which can be passed to the bitcoind nodes managed135by a running `TestShell` object.137## 5. Shutting the `TestShell` down139Shutting down the `TestShell` will safely tear down all running bitcoind140instances and remove all temporary data and logging directories.142```143>>> test.shutdown()14420XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Stopping nodes14520XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Cleaning up /path/to/bitcoin_func_test_XXXXXXX on exit14620XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Tests successful147```148To prevent the logs from being removed after a shutdown, simply set the149`TestShell().options.nocleanup` member to `True`.150```151>>> test.options.nocleanup = True152>>> test.shutdown()15320XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Stopping nodes15420XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Not cleaning up dir /path/to/bitcoin_func_test_XXXXXXX on exit15520XX-XX-XXTXX:XX:XX.XXXXXXX TestFramework (INFO): Tests successful156```158The following utility consolidates logs from the bitcoind nodes and the159underlying `BitcoinTestFramework`:161* `/path/to/bitcoin/build/test/functional/combine_logs.py162'/path/to/bitcoin_func_test_XXXXXXX'`164## 6. Custom `TestShell` parameters166The `TestShell` object initializes with the default settings inherited from the167`BitcoinTestFramework` class. The user can override these in168`TestShell().setup(key=value)`.170**Note:** `TestShell().reset()` will reset test parameters to default values and171can be called after the TestShell is shut down.173| Test parameter key | Default Value | Description |174|---|---|---|175| `bind_to_localhost_only` | `True` | Binds bitcoind P2P services to `127.0.0.1` if set to `True`.|176| `cachedir` | `"/path/to/bitcoin/build/test/cache"` | Sets the bitcoind datadir directory. |177| `chain` | `"regtest"` | Sets the chain-type for the underlying test bitcoind processes. |178| `configfile` | `"/path/to/bitcoin/build/test/config.ini"` | Sets the location of the test framework config file. |179| `coveragedir` | `None` | Records bitcoind RPC test coverage into this directory if set. |180| `loglevel` | `INFO` | Logs events at this level and higher. Can be set to `DEBUG`, `INFO`, `WARNING`, `ERROR` or `CRITICAL`. |181| `nocleanup` | `False` | Cleans up temporary test directory if set to `True` during `shutdown`. |182| `num_nodes` | `1` | Sets the number of initialized bitcoind processes. |183| `rpc_timeout` | `60` | Sets the RPC server timeout for the underlying bitcoind processes. |184| `setup_clean_chain` | `False` | A 200-block-long chain is initialized from cache by default. Instead, `setup_clean_chain` initializes an empty blockchain if set to `True`. |185| `randomseed` | Random Integer | `TestShell().options.randomseed` is a member of `TestShell` which can be accessed during a test to seed a random generator. User can override default with a constant value for reproducible test runs. |186| `supports_cli` | `False` | Whether the bitcoin-cli utility is compiled and available for the test. |187| `tmpdir` | `"/var/folders/.../"` | Sets directory for test logs. Will be deleted upon a successful test run unless `nocleanup` is set to `True` |188| `trace_rpc` | `False` | Logs all RPC calls if set to `True`. |189| `usecli` | `False` | Uses the bitcoin-cli interface for all bitcoind commands instead of directly calling the RPC server. Requires `supports_cli`. |
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.