Full stack Bitcoin software

Test practices

The libbitcoin C++ libraries, from Bitcoin primitives to a complete server.

Test code follows the Style guide except where this page differs. A test file reads as a declarative list of facts, so anything that makes it harder to scan, or that would itself need testing, is excluded.

1. Scope#

Level Scope
unit A method or function, isolated to the extent practical.
component A group of units, such as a class.
functional A feature group, such as client-server communication.

2. Files and structure#

#include "../test.hpp"
#include "../mocks/chunk_storage.hpp"
// record hashmap
// ----------------------------------------------------------------------------

3. Test names#

Name a case subject__method__condition__expectation, joining the parts with double underscores:

BC_AUTO_TEST_CASE(hashmap__enabled__empty_slab_one_bucket__true)
BC_AUTO_TEST_CASE(query_address__get_balance__turbo_block1a_address0_confirmed_blocks__expected)

4. Lines: never wrap inside a test case#

This is the main difference from production code.

// Right.
const auto hash = bitcoin_hash(data);
const auto fingerprint = to_fingerprint(hash);
BC_REQUIRE_EQUAL(fingerprint, expected);

// Wrong.
BC_REQUIRE_EQUAL(to_fingerprint(bitcoin_hash(
    data)), expected);
const hashes roots
{
    test::root01,
    test::root23
};

const auto block = base16_chunk(
    "02000000b51eb76f7cfd89ef4be99b0c7ea3e009ffbf33643661a0fc29354e010000"
    "000029cac78dd1eb1575de815d371bc652c47fcf6f15557bbd02b449513bb46de6f3");

5. No control flow#

Tests contain no if, else, for, while or ternary. This includes fixtures and lambdas inside tests. A test with branches needs a test of its own.

6. No races#

A test never depends on winning a race to pass, however easy it may seem the race is to win. The process does not control scheduling, so a delay of any length is possible, and a test that a delay can fail is wrong.

7. Assertions#

8. Comments#

Comment almost nothing. The test name and the code are the documentation.

9. Pre-commit checklist for agents#

  1. No wraps in any BC_AUTO_TEST_CASE body. This flags continuation lines for review: git diff -U0 -- test | grep -E '^\+\s{8,}\S' | grep -vE '^\+\s*(BC_|//|")'.
  2. No control flow. This should print nothing: git diff -U0 -- test | grep -nE '^\+.*\b(if|for|while|else)\b|\?.*:'.
  3. Names follow subject__method__condition__expectation, at sibling length.
  4. BC_REQUIRE_EQUAL is used wherever the operands are streamable.
  5. Comments. A few at most, none narrative.
  6. A blank line precedes BC_AUTO_TEST_SUITE_END().
  7. No races. No pass depends on scheduling.
  8. The suite passes.

Copyright © 2011-2026 libbitcoin developers