-
Notifications
You must be signed in to change notification settings - Fork 385
Test Guide
Tests have not been fully converted to this style in all repositories.
It is important that tests can be evaluated visually. Otherwise it becomes impossible to determine if the test itself is correct. To achieve this result we apply certain style rules as follows:
This means that test execution is unconditional, there is no branching. Loops, if statements and the ternary operator are prohibited. Test helpers may include conditional logic, but then these should be tested for correctness.
When a test or set of tests fail this helps a developer to quickly isolate the cause. The naming convention is:
BOOST_AUTO_TEST_CASE([class|file]__[method|function]__[condition]__[expectation])
For example:
BOOST_AUTO_TEST_CASE(reservation__stopped__import_last_block__true)
Tests against a method, constructor or function override should use a simple ordinal numbering scheme to group test of the same signature. For example:
BOOST_AUTO_TEST_CASE(configuration__construct1__none_context__expected)
BOOST_AUTO_TEST_CASE(configuration__construct1__mainnet_context__expected)
BOOST_AUTO_TEST_CASE(configuration__construct1__testnet_context__expected)
BOOST_AUTO_TEST_CASE(configuration__construct2__none_context__expected)
Group tests in a single .cpp
file to mirror the source file naming convention. If it becomes necessary to break up the test file into independent files for a single corresponding class or source file, use the following test file naming convention (or a subdirectory):
class__method.cpp
file__function.cpp
Group tests semantically using BOOST_AUTO_TEST_SUITE(...)
. This allows tests to be executed independently. These can be grouped hierarchically if necessary. Generally there is one suite per file or class under test.
Test just one thing. The test name indicates the conditions and expectation of the test. When a test or set of tests fail this allows a developer to quickly isolate the cause. Sometimes it is convenient to test a set of results under one condition, such as a true result code and an expected out value. But these combinations should be semantically associated in that they are necessary to test together. For tests with very costly setup, such as mock databases and network operations, this rule can be bent.
A line of test code should not be wrapped. For this reason line length is not limited as in source files. However intermediate variables can and should be used to limit line length. Structure definitions may be structured across multiple lines.
The BOOST test helpers expose information about the test when there is a failure. It is preferred to use the least general test helper available:
BOOST_REQUIRE(!foo.is_valid())
instead of
BOOST_REQUIRE_EQUAL(foo.is_valid(), false)
If values are serializable then use:
BOOST_REQUIRE_EQUAL(foo.size(), 5u)
as opposed to
BOOST_REQUIRE(foo.size() == 5u)
and in all cases place the condition under test on the left side and the test expectation on the right.
A constexpr
method, function or define should be tested using static_assert
. One that exposes distinct behavior when not constant evaluated (e.g. branching on std::is_constant_evaluated()
) should be tested both using static assertion and under a BOOST test case.
For establishing state before a test and clearing it after, use a BOOST test fixture. For example this creates a cleared directory before each test and clears it after each test.
struct file_utilities_setup_fixture
{
DELETE_COPY_MOVE(file_utilities_setup_fixture);
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
file_utilities_setup_fixture() NOEXCEPT
{
BOOST_REQUIRE(test::clear(test::directory));
}
~file_utilities_setup_fixture() NOEXCEPT
{
BOOST_REQUIRE(test::clear(test::directory));
}
BC_POP_WARNING()
};
BOOST_FIXTURE_TEST_SUITE(file_utilities_tests, file_utilities_setup_fixture)
Use test utilities as opposed to library code, for example this tests only file::clear_directory(...)
:
BOOST_AUTO_TEST_CASE(file_utilities__clear_directory__exists__true_cleared)
{
BOOST_REQUIRE(test::create(TEST_PATH));
BOOST_REQUIRE(test::exists(TEST_PATH));
BOOST_REQUIRE(file::clear_directory(TEST_DIRECTORY));
BOOST_REQUIRE(!test::exists(TEST_PATH));
}
Use test macros as shown above for test base paths.
Users | Developers | License | Copyright © 2011-2024 libbitcoin developers
- Home
- manifesto
- libbitcoin.info
- Libbitcoin Institute
- Freenode (IRC)
- Mailing List
- Slack Channel
- Build Libbitcoin
- Comprehensive Overview
- Developer Documentation
- Tutorials (aaronjaramillo)
- Bitcoin Unraveled
-
Cryptoeconomics
- Foreword by Amir Taaki
- Value Proposition
- Axiom of Resistance
- Money Taxonomy
- Pure Bank
- Production and Consumption
- Labor and Leisure
- Custodial Risk Principle
- Dedicated Cost Principle
- Depreciation Principle
- Expression Principle
- Inflation Principle
- Other Means Principle
- Patent Resistance Principle
- Risk Sharing Principle
- Reservation Principle
- Scalability Principle
- Subjective Inflation Principle
- Consolidation Principle
- Fragmentation Principle
- Permissionless Principle
- Public Data Principle
- Social Network Principle
- State Banking Principle
- Substitution Principle
- Cryptodynamic Principles
- Censorship Resistance Property
- Consensus Property
- Stability Property
- Utility Threshold Property
- Zero Sum Property
- Threat Level Paradox
- Miner Business Model
- Qualitative Security Model
- Proximity Premium Flaw
- Variance Discount Flaw
- Centralization Risk
- Pooling Pressure Risk
- ASIC Monopoly Fallacy
- Auditability Fallacy
- Balance of Power Fallacy
- Blockchain Fallacy
- Byproduct Mining Fallacy
- Causation Fallacy
- Cockroach Fallacy
- Credit Expansion Fallacy
- Debt Loop Fallacy
- Decoupled Mining Fallacy
- Dumping Fallacy
- Empty Block Fallacy
- Energy Exhaustion Fallacy
- Energy Store Fallacy
- Energy Waste Fallacy
- Fee Recovery Fallacy
- Genetic Purity Fallacy
- Full Reserve Fallacy
- Halving Fallacy
- Hoarding Fallacy
- Hybrid Mining Fallacy
- Ideal Money Fallacy
- Impotent Mining Fallacy
- Inflation Fallacy
- Inflationary Quality Fallacy
- Jurisdictional Arbitrage Fallacy
- Lunar Fallacy
- Network Effect Fallacy
- Prisoner's Dilemma Fallacy
- Private Key Fallacy
- Proof of Cost Fallacy
- Proof of Memory Façade
- Proof of Stake Fallacy
- Proof of Work Fallacy
- Regression Fallacy
- Relay Fallacy
- Replay Protection Fallacy
- Reserve Currency Fallacy
- Risk Free Return Fallacy
- Scarcity Fallacy
- Selfish Mining Fallacy
- Side Fee Fallacy
- Split Credit Expansion Fallacy
- Stock to Flow Fallacy
- Thin Air Fallacy
- Time Preference Fallacy
- Unlendable Money Fallacy
- Fedcoin Objectives
- Hearn Error
- Collectible Tautology
- Price Estimation
- Savings Relation
- Speculative Consumption
- Spam Misnomer
- Efficiency Paradox
- Split Speculator Dilemma
- Bitcoin Labels
- Brand Arrogation
- Reserve Definition
- Maximalism Definition
- Shitcoin Definition
- Glossary
- Console Applications
- Development Libraries
- Maintainer Information
- Miscellaneous Articles