Fix incorrect Markdown link
[bitcoinplatinum.git] / test / functional / README.md
blob4d52751b0500ea672e1efc37fe839cf2239fb28e
1 # Functional tests
3 ### Writing Functional Tests
5 #### Example test
7 The [example_test.py](example_test.py) is a heavily commented example of a test case that uses both
8 the RPC and P2P interfaces. If you are writing your first test, copy that file
9 and modify to fit your needs.
11 #### Coverage
13 Running `test_runner.py` with the `--coverage` argument tracks which RPCs are
14 called by the tests and prints a report of uncovered RPCs in the summary. This
15 can be used (along with the `--extended` argument) to find out which RPCs we
16 don't have test cases for.
18 #### Style guidelines
20 - Where possible, try to adhere to [PEP-8 guidelines](https://www.python.org/dev/peps/pep-0008/)
21 - Use a python linter like flake8 before submitting PRs to catch common style
22   nits (eg trailing whitespace, unused imports, etc)
23 - Avoid wildcard imports where possible
24 - Use a module-level docstring to describe what the test is testing, and how it
25   is testing it.
26 - When subclassing the BitcoinTestFramwork, place overrides for the
27   `set_test_params()`, `add_options()` and `setup_xxxx()` methods at the top of
28   the subclass, then locally-defined helper methods, then the `run_test()` method.
30 #### General test-writing advice
32 - Set `self.num_nodes` to the minimum number of nodes necessary for the test.
33   Having additional unrequired nodes adds to the execution time of the test as
34   well as memory/CPU/disk requirements (which is important when running tests in
35   parallel or on Travis).
36 - Avoid stop-starting the nodes multiple times during the test if possible. A
37   stop-start takes several seconds, so doing it several times blows up the
38   runtime of the test.
39 - Set the `self.setup_clean_chain` variable in `set_test_params()` to control whether
40   or not to use the cached data directories. The cached data directories
41   contain a 200-block pre-mined blockchain and wallets for four nodes. Each node
42   has 25 mature blocks (25x50=1250 BTC) in its wallet.
43 - When calling RPCs with lots of arguments, consider using named keyword
44   arguments instead of positional arguments to make the intent of the call
45   clear to readers.
47 #### RPC and P2P definitions
49 Test writers may find it helpful to refer to the definitions for the RPC and
50 P2P messages. These can be found in the following source files:
52 - `/src/rpc/*` for RPCs
53 - `/src/wallet/rpc*` for wallet RPCs
54 - `ProcessMessage()` in `/src/net_processing.cpp` for parsing P2P messages
56 #### Using the P2P interface
58 - `mininode.py` contains all the definitions for objects that pass
59 over the network (`CBlock`, `CTransaction`, etc, along with the network-level
60 wrappers for them, `msg_block`, `msg_tx`, etc).
62 - P2P tests have two threads. One thread handles all network communication
63 with the bitcoind(s) being tested (using python's asyncore package); the other
64 implements the test logic.
66 - `P2PConnection` is the class used to connect to a bitcoind.  `P2PInterface`
67 contains the higher level logic for processing P2P payloads and connecting to
68 the Bitcoin Core node application logic. For custom behaviour, subclass the
69 P2PInterface object and override the callback methods.
71 - Call `network_thread_start()` after all `P2PInterface` objects are created to
72 start the networking thread.  (Continue with the test logic in your existing
73 thread.)
75 - Can be used to write tests where specific P2P protocol behavior is tested.
76 Examples tests are `p2p-accept-block.py`, `p2p-compactblocks.py`.
78 #### Comptool
80 - Comptool is a Testing framework for writing tests that compare the block/tx acceptance
81 behavior of a bitcoind against 1 or more other bitcoind instances. It should not be used
82 to write static tests with known outcomes, since that type of test is easier to write and
83 maintain using the standard BitcoinTestFramework.
85 - Set the `num_nodes` variable (defined in `ComparisonTestFramework`) to start up
86 1 or more nodes.  If using 1 node, then `--testbinary` can be used as a command line
87 option to change the bitcoind binary used by the test.  If using 2 or more nodes,
88 then `--refbinary` can be optionally used to change the bitcoind that will be used
89 on nodes 2 and up.
91 - Implement a (generator) function called `get_tests()` which yields `TestInstance`s.
92 Each `TestInstance` consists of:
93   - A list of `[object, outcome, hash]` entries
94     * `object` is a `CBlock`, `CTransaction`, or
95     `CBlockHeader`.  `CBlock`'s and `CTransaction`'s are tested for
96     acceptance.  `CBlockHeader`s can be used so that the test runner can deliver
97     complete headers-chains when requested from the bitcoind, to allow writing
98     tests where blocks can be delivered out of order but still processed by
99     headers-first bitcoind's.
100     * `outcome` is `True`, `False`, or `None`.  If `True`
101     or `False`, the tip is compared with the expected tip -- either the
102     block passed in, or the hash specified as the optional 3rd entry.  If
103     `None` is specified, then the test will compare all the bitcoind's
104     being tested to see if they all agree on what the best tip is.
105     * `hash` is the block hash of the tip to compare against. Optional to
106     specify; if left out then the hash of the block passed in will be used as
107     the expected tip.  This allows for specifying an expected tip while testing
108     the handling of either invalid blocks or blocks delivered out of order,
109     which complete a longer chain.
110   - `sync_every_block`: `True/False`.  If `False`, then all blocks
111     are inv'ed together, and the test runner waits until the node receives the
112     last one, and tests only the last block for tip acceptance using the
113     outcome and specified tip.  If `True`, then each block is tested in
114     sequence and synced (this is slower when processing many blocks).
115   - `sync_every_transaction`: `True/False`.  Analogous to
116     `sync_every_block`, except if the outcome on the last tx is "None",
117     then the contents of the entire mempool are compared across all bitcoind
118     connections.  If `True` or `False`, then only the last tx's
119     acceptance is tested against the given outcome.
121 - For examples of tests written in this framework, see
122   `invalidblockrequest.py` and `p2p-fullblocktest.py`.
124 ### test-framework modules
126 #### [test_framework/authproxy.py](test_framework/authproxy.py)
127 Taken from the [python-bitcoinrpc repository](https://github.com/jgarzik/python-bitcoinrpc).
129 #### [test_framework/test_framework.py](test_framework/test_framework.py)
130 Base class for functional tests.
132 #### [test_framework/util.py](test_framework/util.py)
133 Generally useful functions.
135 #### [test_framework/mininode.py](test_framework/mininode.py)
136 Basic code to support P2P connectivity to a bitcoind.
138 #### [test_framework/comptool.py](test_framework/comptool.py)
139 Framework for comparison-tool style, P2P tests.
141 #### [test_framework/script.py](test_framework/script.py)
142 Utilities for manipulating transaction scripts (originally from python-bitcoinlib)
144 #### [test_framework/blockstore.py](test_framework/blockstore.py)
145 Implements disk-backed block and tx storage.
147 #### [test_framework/key.py](test_framework/key.py)
148 Wrapper around OpenSSL EC_Key (originally from python-bitcoinlib)
150 #### [test_framework/bignum.py](test_framework/bignum.py)
151 Helpers for script.py
153 #### [test_framework/blocktools.py](test_framework/blocktools.py)
154 Helper functions for creating blocks and transactions.