Skip to content

Commit 2103e12

Browse files
committed
test: Use test framework utils in functional tests
Replaces bare asserts with test framework utils across both the functional tests and the test framework itself. Also adds the `assert_not_equal`, `assert_less_than`, and `assert_less_than_or_equal` util functions for greater readability. Fixes #23119.
1 parent 3e69125 commit 2103e12

File tree

74 files changed

+422
-260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+422
-260
lines changed

test/functional/data/invalid_txs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
MIN_STANDARD_TX_NONWITNESS_SIZE,
5858
script_to_p2sh_script,
5959
)
60+
from test_framework.util import (
61+
assert_equal,
62+
)
6063
basic_p2sh = script_to_p2sh_script(CScript([OP_0]))
6164

6265
class BadTxTemplate:
@@ -124,8 +127,8 @@ def get_tx(self):
124127
tx = CTransaction()
125128
tx.vin.append(self.valid_txin)
126129
tx.vout.append(CTxOut(0, CScript([OP_RETURN] + ([OP_0] * (MIN_PADDING - 2)))))
127-
assert len(tx.serialize_without_witness()) == 64
128-
assert MIN_STANDARD_TX_NONWITNESS_SIZE - 1 == 64
130+
assert_equal(len(tx.serialize_without_witness()), 64)
131+
assert_equal(MIN_STANDARD_TX_NONWITNESS_SIZE - 1, 64)
129132
tx.calc_sha256()
130133
return tx
131134

test/functional/feature_bind_port_discover.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"""
88

99
from test_framework.test_framework import BitcoinTestFramework, SkipTest
10-
from test_framework.util import assert_equal
10+
from test_framework.util import (
11+
assert_equal,
12+
assert_not_equal,
13+
)
1114

1215
# We need to bind to a routable address for this test to exercise the relevant code
1316
# and also must have another routable address on another interface which must not
@@ -71,7 +74,7 @@ def run_test(self):
7174
if local['address'] == ADDR1:
7275
found_addr1 = True
7376
assert_equal(local['port'], BIND_PORT)
74-
assert local['address'] != ADDR2
77+
assert_not_equal(local['address'], ADDR2)
7578
assert found_addr1
7679

7780
if __name__ == '__main__':

test/functional/feature_block.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
from test_framework.test_framework import BitcoinTestFramework
5252
from test_framework.util import (
5353
assert_equal,
54+
assert_less_than,
55+
assert_less_than_or_equal,
5456
assert_greater_than,
5557
)
5658
from test_framework.wallet_util import generate_keypair
@@ -527,7 +529,7 @@ def run_test(self):
527529
b40 = self.next_block(40, spend=out[12])
528530
sigops = get_legacy_sigopcount_block(b40)
529531
numTxes = (MAX_BLOCK_SIGOPS - sigops) // b39_sigops_per_output
530-
assert_equal(numTxes <= b39_outputs, True)
532+
assert_less_than_or_equal(numTxes, b39_outputs)
531533

532534
lastOutpoint = COutPoint(b40.vtx[1].sha256, 0)
533535
new_txs = []
@@ -798,7 +800,7 @@ def run_test(self):
798800
self.move_tip(57)
799801
self.next_block(58, spend=out[17])
800802
tx = CTransaction()
801-
assert len(out[17].vout) < 42
803+
assert_less_than(len(out[17].vout), 42)
802804
tx.vin.append(CTxIn(COutPoint(out[17].sha256, 42), CScript([OP_TRUE]), SEQUENCE_FINAL))
803805
tx.vout.append(CTxOut(0, b""))
804806
tx.calc_sha256()

test/functional/feature_coinstatsindex.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from test_framework.test_framework import BitcoinTestFramework
2929
from test_framework.util import (
3030
assert_equal,
31+
assert_less_than,
32+
assert_not_equal,
3133
assert_raises_rpc_error,
3234
)
3335
from test_framework.wallet import (
@@ -226,7 +228,7 @@ def _test_coin_stats_index(self):
226228

227229
self.generate(index_node, 1, sync_fun=self.no_op)
228230
res10 = index_node.gettxoutsetinfo('muhash')
229-
assert res8['txouts'] < res10['txouts']
231+
assert_less_than(res8['txouts'], res10['txouts'])
230232

231233
self.log.info("Test that the index works with -reindex")
232234

@@ -272,12 +274,12 @@ def _test_reorg_index(self):
272274
res2 = index_node.gettxoutsetinfo(hash_type='muhash', hash_or_height=112)
273275
assert_equal(res["bestblock"], block)
274276
assert_equal(res["muhash"], res2["muhash"])
275-
assert res["muhash"] != res_invalid["muhash"]
277+
assert_not_equal(res["muhash"], res_invalid["muhash"])
276278

277279
# Test that requesting reorged out block by hash is still returning correct results
278280
res_invalid2 = index_node.gettxoutsetinfo(hash_type='muhash', hash_or_height=reorg_block)
279281
assert_equal(res_invalid2["muhash"], res_invalid["muhash"])
280-
assert res["muhash"] != res_invalid2["muhash"]
282+
assert_not_equal(res["muhash"], res_invalid2["muhash"])
281283

282284
# Add another block, so we don't depend on reconsiderblock remembering which
283285
# blocks were touched by invalidateblock

test/functional/feature_csv_activation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from test_framework.test_framework import BitcoinTestFramework
5454
from test_framework.util import (
5555
assert_equal,
56+
assert_greater_than_or_equal,
5657
softfork_active,
5758
)
5859
from test_framework.wallet import (
@@ -131,7 +132,7 @@ def send_generic_input_tx(self, coinbases):
131132
def create_bip68txs(self, bip68inputs, txversion, locktime_delta=0):
132133
"""Returns a list of bip68 transactions with different bits set."""
133134
txs = []
134-
assert len(bip68inputs) >= 16
135+
assert_greater_than_or_equal(len(bip68inputs), 16)
135136
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
136137
locktime = relative_locktime(sdf, srhb, stf, srlb)
137138
tx = self.create_self_transfer_from_utxo(bip68inputs[i])
@@ -145,7 +146,7 @@ def create_bip68txs(self, bip68inputs, txversion, locktime_delta=0):
145146
def create_bip112txs(self, bip112inputs, varyOP_CSV, txversion, locktime_delta=0):
146147
"""Returns a list of bip68 transactions with different bits set."""
147148
txs = []
148-
assert len(bip112inputs) >= 16
149+
assert_greater_than_or_equal(len(bip112inputs), 16)
149150
for i, (sdf, srhb, stf, srlb) in enumerate(product(*[[True, False]] * 4)):
150151
locktime = relative_locktime(sdf, srhb, stf, srlb)
151152
tx = self.create_self_transfer_from_utxo(bip112inputs[i])

test/functional/feature_dbcrash.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
from test_framework.test_framework import BitcoinTestFramework
3838
from test_framework.util import (
3939
assert_equal,
40+
assert_greater_than,
41+
assert_not_equal,
4042
)
4143
from test_framework.wallet import (
4244
MiniWallet,
@@ -274,10 +276,10 @@ def run_test(self):
274276
self.log.info(f"Restarted nodes: {self.restart_counts}; crashes on restart: {self.crashed_on_restart}")
275277

276278
# If no nodes were restarted, we didn't test anything.
277-
assert self.restart_counts != [0, 0, 0]
279+
assert_not_equal(self.restart_counts, [0, 0, 0])
278280

279281
# Make sure we tested the case of crash-during-recovery.
280-
assert self.crashed_on_restart > 0
282+
assert_greater_than(self.crashed_on_restart, 0)
281283

282284
# Warn if any of the nodes escaped restart.
283285
for i in range(3):

test/functional/feature_minchainwork.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
from test_framework.p2p import P2PInterface, msg_getheaders
2121
from test_framework.test_framework import BitcoinTestFramework
22-
from test_framework.util import assert_equal
22+
from test_framework.util import (
23+
assert_equal,
24+
assert_not_equal,
25+
)
2326

2427
# 2 hashes required per regtest block (with no difficulty adjustment)
2528
REGTEST_WORK_PER_BLOCK = 2
@@ -72,7 +75,7 @@ def run_test(self):
7275
assert_equal(len(self.nodes[2].getchaintips()), 1)
7376
assert_equal(self.nodes[2].getchaintips()[0]['height'], 0)
7477

75-
assert self.nodes[1].getbestblockhash() != self.nodes[0].getbestblockhash()
78+
assert_not_equal(self.nodes[1].getbestblockhash(), self.nodes[0].getbestblockhash())
7679
assert_equal(self.nodes[2].getblockcount(), starting_blockcount)
7780

7881
self.log.info("Check that getheaders requests to node2 are ignored")

test/functional/feature_posix_fs_permissions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import stat
1010

1111
from test_framework.test_framework import BitcoinTestFramework
12+
from test_framework.util import assert_equal
1213

1314

1415
class PosixFsPermissionsTest(BitcoinTestFramework):
@@ -22,12 +23,12 @@ def skip_test_if_missing_module(self):
2223
def check_directory_permissions(self, dir):
2324
mode = os.lstat(dir).st_mode
2425
self.log.info(f"{stat.filemode(mode)} {dir}")
25-
assert mode == (stat.S_IFDIR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
26+
assert_equal(mode, (stat.S_IFDIR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR))
2627

2728
def check_file_permissions(self, file):
2829
mode = os.lstat(file).st_mode
2930
self.log.info(f"{stat.filemode(mode)} {file}")
30-
assert mode == (stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR)
31+
assert_equal(mode, (stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR))
3132

3233
def run_test(self):
3334
self.stop_node(0)

test/functional/feature_pruning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def reorg_test(self):
197197
self.nodes[1].invalidateblock(curhash)
198198
curhash = self.nodes[1].getblockhash(self.forkheight - 1)
199199

200-
assert self.nodes[1].getblockcount() == self.forkheight - 1
200+
assert_equal(self.nodes[1].getblockcount(), self.forkheight - 1)
201201
self.log.info(f"New best height: {self.nodes[1].getblockcount()}")
202202

203203
# Disconnect node1 and generate the new chain

test/functional/feature_rbf.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from test_framework.test_framework import BitcoinTestFramework
1515
from test_framework.util import (
1616
assert_equal,
17+
assert_greater_than,
18+
assert_less_than,
19+
assert_less_than_or_equal,
1720
assert_raises_rpc_error,
1821
)
1922
from test_framework.wallet import MiniWallet
@@ -102,7 +105,7 @@ def make_utxo(self, node, amount, *, confirmed=True, scriptPubKey=None):
102105
new_size = len(node.getrawmempool())
103106
# Error out if we have something stuck in the mempool, as this
104107
# would likely be a bug.
105-
assert new_size < mempool_size
108+
assert_less_than(new_size, mempool_size)
106109
mempool_size = new_size
107110

108111
return self.wallet.get_utxo(txid=tx["txid"], vout=tx["sent_vout"])
@@ -445,9 +448,9 @@ def test_too_many_replacements_with_default_mempool_params(self):
445448
num_txs_invalidated = len(root_utxos) + (num_tx_graphs * txs_per_graph)
446449

447450
if failure_expected:
448-
assert num_txs_invalidated > MAX_REPLACEMENT_LIMIT
451+
assert_greater_than(num_txs_invalidated, MAX_REPLACEMENT_LIMIT)
449452
else:
450-
assert num_txs_invalidated <= MAX_REPLACEMENT_LIMIT
453+
assert_less_than_or_equal(num_txs_invalidated, MAX_REPLACEMENT_LIMIT)
451454

452455
# Now attempt to submit a tx that double-spends all the root tx inputs, which
453456
# would invalidate `num_txs_invalidated` transactions.

0 commit comments

Comments
 (0)