bitcoin/src/test
fanquake 62d9ff9e6e
Merge bitcoin/bitcoin#22107: scripted-diff: rename GetSystemTimeInSeconds to GetTimeSeconds
feb72e5432 scripted-diff: rename GetSystemTimeInSeconds to GetTimeSeconds (fanquake)

Pull request description:

  This PR simply renames `GetSystemTimeInSeconds` to `GetTimeSeconds`, for uniformity amongst our time handling functions (`GetTimeMillis`, `GetTimeMicros`). I have a branch that does a chunk of `GetTime()` -> `GetSystemTimeInSeconds` (`GetTimeSeconds`) / `GetTime<T>` migration, so we can eventually remove the (2 year) deprecated `GetTime()`.

  However, splitting this off and doing the renaming first while the number of `GetSystemTimeInSeconds` instances is small seems worthwhile.

ACKs for top commit:
  practicalswift:
    cr ACK feb72e5432: patch looks correct
  promag:
    Code review ACK feb72e5432.

Tree-SHA512: e2ac30be9cbcd77b70c9f74bef820b558945d0fcc6f3dc59fde68a18d08a7d36f42088b804ffe7c03478c8db048615b4c4aa65a3d8d9f5d717d59b58c99f1c54
2021-06-01 11:07:39 +08:00
..
data test: Add missing test for empty P2WSH redeem 2021-05-01 10:12:11 +02:00
fuzz Merge bitcoin/bitcoin#20833: rpc/validation: enable packages through testmempoolaccept 2021-05-27 22:40:24 +02:00
util Merge bitcoin/bitcoin#20833: rpc/validation: enable packages through testmempoolaccept 2021-05-27 22:40:24 +02:00
addrman_tests.cpp p2p: allow CAddrMan::GetAddr() by network, add doxygen 2021-05-19 13:04:11 +02:00
allocator_tests.cpp
amount_tests.cpp refactor: Make CFeeRate constructor architecture-independent 2021-05-18 07:13:25 +02:00
arith_uint256_tests.cpp
base32_tests.cpp
base58_tests.cpp refactor: Remove useless extern keyword 2021-05-10 09:09:24 +02:00
base64_tests.cpp
bech32_tests.cpp
bip32_tests.cpp Additional test vector for hardened derivation with leading zeros 2021-05-28 17:22:01 +03:00
blockchain_tests.cpp
blockencodings_tests.cpp
blockfilter_index_tests.cpp index: Avoid async shutdown on init error 2021-05-12 10:47:35 +02:00
blockfilter_tests.cpp
bloom_tests.cpp
bswap_tests.cpp test: remove qt byteswap compattests 2021-03-29 11:12:26 +08:00
checkqueue_tests.cpp
coins_tests.cpp
coinstatsindex_tests.cpp index: Avoid async shutdown on init error 2021-05-12 10:47:35 +02:00
compilerbug_tests.cpp
compress_tests.cpp refactor: Replace &foo[0] with foo.data() 2021-05-04 06:55:31 +02:00
crypto_tests.cpp refactor: Replace &foo[0] with foo.data() 2021-05-04 06:55:31 +02:00
cuckoocache_tests.cpp
dbwrapper_tests.cpp scripted-diff: Change ArgsManager.GetDataDirPath() to ArgsManager.GetDataDirBase() in tests 2021-05-24 10:29:57 +02:00
denialofservice_tests.cpp scripted-diff: Change ArgsManager.GetDataDirPath() to ArgsManager.GetDataDirBase() in tests 2021-05-24 10:29:57 +02:00
descriptor_tests.cpp script: allow up to 20 keys in wsh() descriptors 2021-04-28 10:00:29 +02:00
flatfile_tests.cpp scripted-diff: Change ArgsManager.GetDataDirPath() to ArgsManager.GetDataDirBase() in tests 2021-05-24 10:29:57 +02:00
fs_tests.cpp scripted-diff: Change ArgsManager.GetDataDirPath() to ArgsManager.GetDataDirBase() in tests 2021-05-24 10:29:57 +02:00
getarg_tests.cpp scripted-diff: Replace m_args with m_local_args in getarg_tests.cpp 2021-04-18 11:59:28 +02:00
hash_tests.cpp
i2p_tests.cpp scripted-diff: Replace GetDataDir() calls with gArgs.GetDataDirNet() calls 2021-05-24 10:29:58 +02:00
interfaces_tests.cpp
key_io_tests.cpp refactor: Remove useless extern keyword 2021-05-10 09:09:24 +02:00
key_tests.cpp
logging_tests.cpp test: Reset mocktime in the common setup 2021-04-14 17:38:07 +02:00
main.cpp
Makefile
mempool_tests.cpp test: Reset mocktime in the common setup 2021-04-14 17:38:07 +02:00
merkle_tests.cpp
merkleblock_tests.cpp
miner_tests.cpp [validation] make CheckSequenceLocks context-free 2021-05-20 21:34:31 +01:00
multisig_tests.cpp
net_peer_eviction_tests.cpp
net_tests.cpp net: Return IPv6 scope id in CNetAddr::ToStringIP() 2021-05-18 21:01:32 +02:00
netbase_tests.cpp scripted-diff: rename NetPermissionFlags enumerators 2021-05-12 16:13:30 +02:00
pmt_tests.cpp
policy_fee_tests.cpp
policyestimator_tests.cpp
pow_tests.cpp
prevector_tests.cpp
raii_event_tests.cpp
random_tests.cpp
README.md
reverselock_tests.cpp
rpc_tests.cpp test: Reset mocktime in the common setup 2021-04-14 17:38:07 +02:00
sanity_tests.cpp
scheduler_tests.cpp
script_p2sh_tests.cpp
script_standard_tests.cpp refactor: Replace &foo[0] with foo.data() 2021-05-04 06:55:31 +02:00
script_tests.cpp refactor: Remove useless extern keyword 2021-05-10 09:09:24 +02:00
scriptnum10.h
scriptnum_tests.cpp
serfloat_tests.cpp Convert existing float encoding tests 2021-05-24 16:15:02 -07:00
serialize_tests.cpp Remove support for double serialization 2021-05-24 16:15:05 -07:00
settings_tests.cpp scripted-diff: Change ArgsManager.GetDataDirPath() to ArgsManager.GetDataDirBase() in tests 2021-05-24 10:29:57 +02:00
sighash_tests.cpp refactor: Remove useless extern keyword 2021-05-10 09:09:24 +02:00
sigopcount_tests.cpp
skiplist_tests.cpp
sock_tests.cpp net: flag relevant Sock methods with [[nodiscard]] 2021-04-13 17:26:20 +02:00
streams_tests.cpp streams: Accept URef obj for VectorReader unserialize 2021-05-05 20:19:03 +02:00
sync_tests.cpp
system_tests.cpp
timedata_tests.cpp
torcontrol_tests.cpp
transaction_tests.cpp refactor: Remove useless extern keyword 2021-05-10 09:09:24 +02:00
txindex_tests.cpp index: Avoid async shutdown on init error 2021-05-12 10:47:35 +02:00
txrequest_tests.cpp
txvalidation_tests.cpp [policy] limit package sizes 2021-05-24 14:42:10 +01:00
txvalidationcache_tests.cpp
uint256_tests.cpp
util_tests.cpp scripted-diff: rename GetSystemTimeInSeconds to GetTimeSeconds 2021-05-31 15:11:18 +08:00
util_threadnames_tests.cpp
validation_block_tests.cpp miner: Add chainstate member to BlockAssembler 2021-03-24 15:40:56 -04:00
validation_chainstate_tests.cpp
validation_chainstatemanager_tests.cpp refactor: Avoid magic value of all-zeros in assumeutxo base_blockhash 2021-05-11 11:21:05 +02:00
validation_flush_tests.cpp
validation_tests.cpp fuzz: Add utxo_snapshot target 2021-05-16 11:34:27 +02:00
validationinterface_tests.cpp
versionbits_tests.cpp test: Run versionbits_sanity for all chains 2021-04-17 11:29:50 +02:00

Unit tests

The sources in this directory are unit test cases. Boost includes a unit testing framework, and since Bitcoin Core already uses Boost, it makes sense to simply use this framework rather than require developers to configure some other framework (we want as few impediments to creating unit tests as possible).

The build system is set up to compile an executable called test_bitcoin that runs all of the unit tests. The main source file for the test library is found in util/setup_common.cpp.

Compiling/running unit tests

Unit tests will be automatically compiled if dependencies were met in ./configure and tests weren't explicitly disabled.

After configuring, they can be run with make check.

To run the unit tests manually, launch src/test/test_bitcoin. To recompile after a test file was modified, run make and then run the test again. If you modify a non-test file, use make -C src/test to recompile only what's needed to run the unit tests.

To add more unit tests, add BOOST_AUTO_TEST_CASE functions to the existing .cpp files in the test/ directory or add new .cpp files that implement new BOOST_AUTO_TEST_SUITE sections.

To run the GUI unit tests manually, launch src/qt/test/test_bitcoin-qt

To add more GUI unit tests, add them to the src/qt/test/ directory and the src/qt/test/test_main.cpp file.

Running individual tests

test_bitcoin has some built-in command-line arguments; for example, to run just the getarg_tests verbosely:

test_bitcoin --log_level=all --run_test=getarg_tests -- DEBUG_LOG_OUT

log_level controls the verbosity of the test framework, which logs when a test case is entered, for example. The DEBUG_LOG_OUT after the two dashes redirects the debug log, which would normally go to a file in the test datadir (BasicTestingSetup::m_path_root), to the standard terminal output.

... or to run just the doubledash test:

test_bitcoin --run_test=getarg_tests/doubledash

Run test_bitcoin --help for the full list.

Adding test cases

To add a new unit test file to our test suite you need to add the file to src/Makefile.test.include. The pattern is to create one test file for each class or source file for which you want to create unit tests. The file naming convention is <source_filename>_tests.cpp and such files should wrap their tests in a test suite called <source_filename>_tests. For an example of this pattern, see uint256_tests.cpp.

Logging and debugging in unit tests

make check will write to a log file foo_tests.cpp.log and display this file on failure. For running individual tests verbosely, refer to the section above.

To write to logs from unit tests you need to use specific message methods provided by Boost. The simplest is BOOST_TEST_MESSAGE.

For debugging you can launch the test_bitcoin executable with gdbor lldb and start debugging, just like you would with any other program:

gdb src/test/test_bitcoin