Skip to content

qa: assert_equal -> assert_true/assert_false - #36257

Open
hodlinator wants to merge 6 commits into
bitcoin:masterfrom
hodlinator:2026/09/assert_true
Open

hodlinator wants to merge 6 commits into
bitcoin:masterfrom
hodlinator:2026/09/assert_true

Conversation

@hodlinator

@hodlinator hodlinator commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Instead of this in the functional tests:

    assert_equal([long expression pushing next arg to the right], True)
    assert_equal([other long expression pushing next arg to the right], False)

we get:

    assert_true([long expression without arg on the right side..])
    assert_false([other long expression without next arg to the right.])

The main point of this change is that assert_true()/assert_false() reduce cognitive tokens when scanning the code (equals to what...?). (As a consequence, assert_true()/assert_false() are also common in other test frameworks).

assert_equal() continues to be used in cases where we don't compare to boolean literals.

assert_true([expression]) is different from plain assert [expression] in that assert is truthy while assert_true requires boolean True. (assert can also be deactivated when executing Python code in optimized mode (see #30529 (comment))).

A smaller scale attempt at introducing assert_true() was originally included in #31874 before being dropped.

@DrahtBot DrahtBot added the Tests label Sep 15, 2026
@DrahtBot

DrahtBot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36990 (test: replace hard-coded RPC error codes by l0rinc)
  • #36976 (rpc: Use OBJ_NAMED_PARAMS in estimatesmartfee by maflcko)
  • #36969 (net: Warn only once when a proxy or the I2P proxy is unreachable by fjahr)
  • #36968 (test/mutation: check wallet listings and wrapped P2WSH addresses in wallet_migration.py by l0rinc)
  • #36891 (test: fix intermittent failure in p2p_tx_download.py by Crypt-iQ)
  • #36796 (wallet, sqlite: Enable secure_delete by default by achow101)
  • #36648 (descriptor, rpc, wallet: do not echo key material in error messages by fametrano)
  • #36627 (tracing: run USDT tests without BPF privileges by willcl-ark)
  • #36540 (mempool: remove time-based transaction expiry by ismaelsadeeq)
  • #36525 (mining: add Block interface, fix template data race by Sjors)
  • #36482 (wallet: Only broadcast resubmitted transactions that were added to the mempool by achow101)
  • #36402 (refactor: wallet_importdescriptors to separate run_test() contents into specific functions by nebula-21)
  • #35920 (net_processing: Ignore MSG_WITNESS_TX entries from INV messages by ajtowns)
  • #35433 (wallet: deprecate replaceable argument from transaction (and psbt) creation (and modification) RPCs by rkrux)
  • #35377 (wallet: Allow importing of descriptors without private keys when the wallet has the private keys by achow101)
  • #35355 (Use atomics for determining whether trace logging is enabled by ajtowns)
  • #34872 (wallet: fix mixed-input transaction accounting in history RPCs by w0xlt)
  • #34038 (logging: replace -loglevel with -trace, expose trace logging via RPC by ajtowns)
  • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors)
  • #33112 (wallet: relax external_signer flag constraints by Sjors)
  • #32857 (wallet: allow skipping script paths by Sjors)
  • #31668 (Added rescan option for import descriptors by saikiran57)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)
  • #27260 (Enhanced error messages for invalid network prefix during address parsing. by portlandhodl)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@l0rinc l0rinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the change, the only reason I see to use explicit True and False is to flip these after a characterization test - but they could also flip the method name.

Based on https://mirror.b10c.me/bitcoin-bitcoin/34773#discussion_r3009767675 and https://mirror.b10c.me/bitcoin-bitcoin/34773#discussion_r3009771746, @maflcko might be interested.

Comment thread test/functional/test_framework/util.py Resolved

@hodlinator hodlinator left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the change, the only reason I see to use explicit True and False is to flip these after a characterization test - but they could also flip the method name.

It was reviewing your characterization test flipping which made me flip and implement this. :)

Comment thread test/functional/test_framework/util.py Resolved
@maflcko

maflcko commented Sep 16, 2026

Copy link
Copy Markdown
Member

assert_true([expression]) is different from plain assert [expression] in that the latter can be deactivated when executing Python code in optimized mode (see #30529 (comment)).

This isn't wrong, but seems to be missing the point: The reason why assert_equal with True or False is used is not because assert may dropped (in theory, no one is doing this in practise?), but rather that the assert only checks truthy/falsy states, not type-safe equality.

So the only real remaining motivation for this is to reduce code bloat. No objection, but overall I am ~0 on this.

@hodlinator

Copy link
Copy Markdown
Contributor Author

Thanks for having a look @maflcko. Re-orged the PR desc to add the truthyness nuance of assert and also to make the cognitive part more prominent as the motivation.

@hodlinator
hodlinator force-pushed the 2026/09/assert_true branch from 01ff43d to 77e6026 Compare October 1, 2026 12:48
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/43847803530/job/133232294579
LLM reason (✨ experimental): CI failed the scripted-diff lint: its guard found existing assert_true/assert_false calls in test/functional.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@hodlinator

Copy link
Copy Markdown
Contributor Author

Sure, if stuff is free, we could do all of it. However, #34395 seems more useful than this and it didn't make it either.

Agree both are very low-priority, but it seems quite subjective which one is more useful (abbreviating the names of functions vs removing begging-the-question tokens (equals ...to arbitrarily long expression ... False/True)).

Liked how that PR added a guard in the scripted diff against the new names, added a commit here doing the same before introducing the new functions.

-BEGIN VERIFY SCRIPT-
git grep -E "\b(assert_true|assert_false)\b" -- test/functional && echo "Error: Target names already exist" >&2 && exit 1 || exit 0
-END VERIFY SCRIPT-
Before 2ef6679 introduced finalized2 we had:
assert_equal(finalized["complete"], True)
...so this is changing things back towards that.
…ssert_true/assert_false

-BEGIN VERIFY SCRIPT-
sed -i -E "s/assert_equal\((.*)\, (True|False)\)( *(#.*))?$/assert_\L\2(\E\1)\3/g" $( git grep -lE "assert_equal\(.*, (True|False)\)"  -- test/functional/ )
sed -i -E "s/assert_equal\((True|False), (.*)$/assert_\L\1\E\(\2/g" $( git grep -lE "assert_equal\((True|False)"  -- test/functional/ )
-END VERIFY SCRIPT-
from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_node import ErrorMatch
from test_framework.util import assert_equal
from test_framework.util import assert_equal, assert_false, assert_true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

@hodlinator

Copy link
Copy Markdown
Contributor Author

@maflcko I checked your approach in #35139, where you only have ASSERT() rather than ASSERT_TRUE()/ASSERT_FALSE(). One could introduce just one custom non-truthy assert_(arg) function for Python too. The problem with that is I couldn't find a type safe way of negating a boolean aside from also introducing a custom not_(arg) function.

@maflcko

maflcko commented Nov 24, 2026

Copy link
Copy Markdown
Member

I don't think #35139 is going to make it any time soon, it is marked as DRAFT , mostly meant as a tracking issue for the bugs, but maybe I should close it. Also, for reference it has #define ASSERT_EQ(l, r) detail::check_op(l, r, #l " == " #r, "!=", std::equal_to<>{}), not only a single one.

Sure, if stuff is free, we could do all of it. However, #34395 seems more useful than this and it didn't make it either.

Agree both are very low-priority, but it seems quite subjective which one is more useful (abbreviating the names of functions vs removing begging-the-question tokens (equals ...to arbitrarily long expression ... False/True)).

Personally, I sometimes use assert_equal(True, long...expr) ...

@hodlinator hodlinator changed the title qa: assert_equals -> assert_true/assert_false qa: assert_equal -> assert_true/assert_false Nov 24, 2026
@hodlinator

Copy link
Copy Markdown
Contributor Author

Also, for reference it has #define ASSERT_EQ(l, r) detail::check_op(l, r, #l " == " #r, "!=", std::equal_to<>{}), not only a single one.

Yes, I'm all for keeping assert_equal for comparisons against non-literals.

Personally, I sometimes use assert_equal(True, long...expr) ...

One could create a scripted diff for switching existing cases of assert_equal(long...expr, True) to assert_equal(True, long...expr) (not that you have advocated for it). But the current PR approach seems like a better path forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants