Skip to content

Commit

Permalink
[tests] don't override __init__() in individual tests
Browse files Browse the repository at this point in the history
Almost all test scripts currently need to override the __init__()
method. When they do that they need to call into super().__init__() as
the base class does some generic initialization.

This commit makes the base class __init__() call into set_test_params()
method. Individual test cases can override set_test_params() to setup
their test parameters.
  • Loading branch information
jnewbery committed Sep 1, 2017
1 parent 6cf094a commit 5448a14
Show file tree
Hide file tree
Showing 82 changed files with 145 additions and 322 deletions.
6 changes: 3 additions & 3 deletions test/functional/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ don't have test cases for.
- Use a module-level docstring to describe what the test is testing, and how it
is testing it.
- When subclassing the BitcoinTestFramwork, place overrides for the
`__init__()`, and `setup_xxxx()` methods at the top of the subclass, then
locally-defined helper methods, then the `run_test()` method.
`set_test_params()`, `add_options()` and `setup_xxxx()` methods at the top of
the subclass, then locally-defined helper methods, then the `run_test()` method.

#### General test-writing advice

Expand All @@ -36,7 +36,7 @@ don't have test cases for.
- Avoid stop-starting the nodes multiple times during the test if possible. A
stop-start takes several seconds, so doing it several times blows up the
runtime of the test.
- Set the `self.setup_clean_chain` variable in `__init__()` to control whether
- Set the `self.setup_clean_chain` variable in `set_test_params()` to control whether
or not to use the cached data directories. The cached data directories
contain a 200-block pre-mined blockchain and wallets for four nodes. Each node
has 25 mature blocks (25x50=1250 BTC) in its wallet.
Expand Down
4 changes: 1 addition & 3 deletions test/functional/abandonconflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
from test_framework.util import *

class AbandonConflictTest(BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = False
self.extra_args = [["-minrelaytxfee=0.00001"], []]

def run_test(self):
Expand Down
3 changes: 1 addition & 2 deletions test/functional/assumevalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def send_header_for_blocks(self, new_blocks):
self.send_message(headers_message)

class AssumeValidTest(BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 3

Expand Down
4 changes: 1 addition & 3 deletions test/functional/bip65-cltv-p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def create_transaction(node, coinbase, to_address, amount):
return tx

class BIP65Test(BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 1
self.extra_args = [['-promiscuousmempoolflags=1', '-whitelist=127.0.0.1']]
self.setup_clean_chain = True
Expand Down
4 changes: 2 additions & 2 deletions test/functional/bip68-112-113-p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def all_rlt_txs(txarray):
return txs

class BIP68_112_113Test(ComparisonTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [['-whitelist=127.0.0.1', '-blockversion=4']]

def run_test(self):
Expand Down
4 changes: 1 addition & 3 deletions test/functional/bip68-sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
NOT_FINAL_ERROR = "64: non-BIP68-final"

class BIP68Test(BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = False
self.extra_args = [[], ["-acceptnonstdtxn=0"]]

def run_test(self):
Expand Down
5 changes: 2 additions & 3 deletions test/functional/bip9-softforks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKSEQUENCEVERIFY, OP_DROP

class BIP9SoftForksTest(ComparisonTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 1
self.extra_args = [['-whitelist=127.0.0.1']]
self.setup_clean_chain = True

def run_test(self):
self.test = TestManager(self, self.options.tmpdir)
Expand Down
4 changes: 1 addition & 3 deletions test/functional/bipdersig-p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def create_transaction(node, coinbase, to_address, amount):
return tx

class BIP66Test(BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 1
self.extra_args = [['-promiscuousmempoolflags=1', '-whitelist=127.0.0.1']]
self.setup_clean_chain = True
Expand Down
6 changes: 1 addition & 5 deletions test/functional/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@
assert_is_hash_string,
)


class BlockchainTest(BitcoinTestFramework):

def __init__(self):
super().__init__()
self.setup_clean_chain = False
def set_test_params(self):
self.num_nodes = 1
self.extra_args = [['-stopatheight=207']]

Expand Down
3 changes: 1 addition & 2 deletions test/functional/bumpfee.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@


class BumpFeeTest(BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True
self.extra_args = [["-prematurewitness", "-walletprematurewitness", "-walletrbf={}".format(i)]
Expand Down
6 changes: 2 additions & 4 deletions test/functional/create_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
from test_framework.test_framework import BitcoinTestFramework

class CreateCache(BitcoinTestFramework):
# Test network and test nodes are not required:

def __init__(self):
super().__init__()

# Test network and test nodes are not required:
def set_test_params(self):
self.num_nodes = 0

def setup_network(self):
Expand Down
3 changes: 1 addition & 2 deletions test/functional/dbcrash.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
pass

class ChainstateWriteCrashTest(BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 4
self.setup_clean_chain = False

Expand Down
4 changes: 1 addition & 3 deletions test/functional/decodescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
from io import BytesIO

class DecodeScriptTest(BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1

Expand Down
5 changes: 1 addition & 4 deletions test/functional/disablewallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *


class DisableWalletTest (BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1
self.extra_args = [["-disablewallet"]]
Expand Down
5 changes: 1 addition & 4 deletions test/functional/disconnect_ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
)

class DisconnectBanTest(BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = False

def run_test(self):
self.log.info("Test setban and listbanned RPCs")
Expand Down
10 changes: 3 additions & 7 deletions test/functional/example_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,11 @@ def custom_function():
class ExampleTest(BitcoinTestFramework):
# Each functional test is a subclass of the BitcoinTestFramework class.

# Override the __init__(), add_options(), setup_chain(), setup_network()
# Override the set_test_params(), add_options(), setup_chain(), setup_network()
# and setup_nodes() methods to customize the test setup as required.

def __init__(self):
"""Initialize the test
Call super().__init__() first, and then override any test parameters
for your individual test."""
super().__init__()
def set_test_params(self):
"""Override any test parameters for your individual test."""
self.setup_clean_chain = True
self.num_nodes = 3
# Use self.extra_args to change command-line arguments for the nodes
Expand Down
5 changes: 1 addition & 4 deletions test/functional/forknotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
from test_framework.test_framework import BitcoinTestFramework

class ForkNotifyTest(BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = False

def setup_network(self):
self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
Expand Down
6 changes: 1 addition & 5 deletions test/functional/fundrawtransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ def get_unspent(listunspent, amount):
return utx
raise AssertionError('Could not find unspent with amount={}'.format(amount))


class RawTransactionsTest(BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 4

def setup_network(self, split=False):
self.setup_nodes()
Expand Down
5 changes: 0 additions & 5 deletions test/functional/getblocktemplate_longpoll.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ def run(self):
self.node.getblocktemplate({'longpollid':self.longpollid})

class GetBlockTemplateLPTest(BitcoinTestFramework):
def __init__(self):
super().__init__()
self.num_nodes = 4
self.setup_clean_chain = False

def run_test(self):
self.log.info("Warning: this test will take about 70 seconds in the best case. Be patient.")
self.nodes[0].generate(10)
Expand Down
6 changes: 0 additions & 6 deletions test/functional/getchaintips.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
from test_framework.util import assert_equal

class GetChainTipsTest (BitcoinTestFramework):
def __init__(self):
super().__init__()
self.num_nodes = 4
self.setup_clean_chain = False

def run_test (self):

tips = self.nodes[0].getchaintips ()
assert_equal (len (tips), 1)
assert_equal (tips[0]['branchlen'], 0)
Expand Down
4 changes: 1 addition & 3 deletions test/functional/httpbasics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import urllib.parse

class HTTPBasicsTest (BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 3
self.setup_clean_chain = False

def setup_network(self):
self.setup_nodes()
Expand Down
3 changes: 1 addition & 2 deletions test/functional/import-rescan.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ def check(self, txid=None, amount=None, confirmations=None):


class ImportRescanTest(BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 2 + len(IMPORT_NODES)

def setup_network(self):
Expand Down
3 changes: 1 addition & 2 deletions test/functional/importmulti.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from test_framework.util import *

class ImportMultiTest (BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True

Expand Down
5 changes: 1 addition & 4 deletions test/functional/importprunedfunds.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *


class ImportPrunedFundsTest(BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2

Expand Down
4 changes: 1 addition & 3 deletions test/functional/invalidateblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from test_framework.util import *

class InvalidateTest(BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 3

Expand Down
4 changes: 2 additions & 2 deletions test/functional/invalidblockrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class InvalidBlockRequestTest(ComparisonTestFramework):

''' Can either run this test as 1 node with expected answers, or two and compare them.
Change the "outcome" variable from each TestInstance object to only do the comparison. '''
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True

def run_test(self):
test = TestManager(self, self.options.tmpdir)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/invalidtxrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class InvalidTxRequestTest(ComparisonTestFramework):

''' Can either run this test as 1 node with expected answers, or two and compare them.
Change the "outcome" variable from each TestInstance object to only do the comparison. '''
def __init__(self):
super().__init__()
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True

def run_test(self):
test = TestManager(self, self.options.tmpdir)
Expand Down
3 changes: 1 addition & 2 deletions test/functional/keypool-topup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
)

class KeypoolRestoreTest(BitcoinTestFramework):
def __init__(self):
super().__init__()
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2
self.extra_args = [['-usehd=0'], ['-usehd=1', '-keypool=100', '-keypoolmin=20']]
Expand Down
7 changes: 2 additions & 5 deletions test/functional/keypool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from test_framework.util import *

class KeyPoolTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1

def run_test(self):
nodes = self.nodes
Expand Down Expand Up @@ -78,10 +80,5 @@ def run_test(self):
assert_equal(wi['keypoolsize_hd_internal'], 100)
assert_equal(wi['keypoolsize'], 100)

def __init__(self):
super().__init__()
self.setup_clean_chain = False
self.num_nodes = 1

if __name__ == '__main__':
KeyPoolTest().main()
5 changes: 1 addition & 4 deletions test/functional/listsinceblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
from test_framework.util import assert_equal

class ListSinceBlockTest (BitcoinTestFramework):

def __init__(self):
super().__init__()
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 4

def run_test(self):
self.nodes[2].generate(101)
Expand Down
5 changes: 1 addition & 4 deletions test/functional/listtransactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ def txFromHex(hexstring):
return tx

class ListTransactionsTest(BitcoinTestFramework):
def __init__(self):
super().__init__()
self.num_nodes = 4
self.setup_clean_chain = False
def set_test_params(self):
self.enable_mocktime()

def run_test(self):
Expand Down
Loading

0 comments on commit 5448a14

Please sign in to comment.