Repository navigation
Remove boost as a unit test runner - #35713
rustaceanrob wants to merge 7 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
|
Moving over my original ACK from the previous PR: #35587 (comment) Strong Concept ACK! Love to see it. While I broadly agree with the author's reasons stated in the PR, the ones I'll highlight specifically are:
Having our own simple, in house test framework gives us the ability to customise it for exactly our needs. I believe we should do everything in our power to make testing an easy, intuitive, and feature rich experience as this helps to remove barriers to people writing and interpreting tests. In particular, as a longer term project I am looking at ways to improve concurrency in our testing to help speed up CI. As @purpleKarrot points out:
|
|
Draft while I work on #35713 (comment) |
00f8712 to
16926e7
Compare
f812859 to
19e7983
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
Concept ACK |
|
@rustaceanrob was interested in seeing how the changes would affect compatibility with common IDEs. So I took a quick look at CLion, which is what I primarily use, to see how it would be affected by the changes. For individual tests to be detected by the IDE and appear as runnable directly from the test file, the macros have to expand into code from a framework supported by the IDE (Boost.Test being one of them). This is why, for example, the following play buttons would disappear after the changes introduced by this PR:
However, since CTest is still supported by the IDE, we're still able to use the features CLion provides for it, including adding run configurations and running test suites directly from the CMake files:
|
|
Concept ACK |
|
ea7d900 addressed Ralph, simplified the argument parsing a bit, and dropped an unneeded depends patch edit: Ralphs current comments I am going to put off as low priority or obvious misuse. |
This commit introduces a new test runner as well as the associated macros. The runner takes inspiration from boost, catch2, and doctest. Notably, expressions are "decomposed" by taking each value and recording a debug string in the case of an invalid expression. It does so by overriding an operator (<=) before other comparison operations are evaluated. Each test initializes a `TestContext` that manages the test state and scoped logs. The `TestContext` requires thread awareness to manage scoped logs in potentially many threads. `REQUIRE` thrown on child threads is also invalid because the exception cannot propagate to the main thread. With respect to the comparison checks in `CapturedExpression`, C style strings and integers have special handling. C style stirngs are compared by the value of the string, not the memory address, which is the same behavior as boost. Integers with mixed signs are compared using `std::cmp_*`, which is a deliberate deviation from boost. This removes potential underflows or overflows similar to bitcoin#35689. All other types are compared by the comparison operator. Compatiblity with boost is maintained for environment variables and CLI arguments that are used in CI and docs. `new_main.cpp` is defined here so reviewers can easily look at the file, but it is renamed to `main.cpp` in the following commit. ref: https://fekir.info/post/decomposing-an-expression/
This migrates the test runner, boost header imports, and linter. `include <memory>` line is to fix an IWYU. -BEGIN VERIFY SCRIPT- mv src/test/new_main.cpp src/test/main.cpp git grep -l '<boost/test/unit_test.hpp>' | xargs sed -i 's|<boost/test/unit_test.hpp>|<test/util/framework.h>|' sed -i '/SIGSTKSZ/d; /BOOST_TEST_DISABLE_ALT_STACK/d; /BOOST_TEST_MODULE/d; s|<boost/test/included/unit_test.hpp>|<test/util/framework.h>|' src/test/kernel/test_kernel.cpp sed -i 's|boost::unit_test::framework::master_test_suite().argv\[0\]|framework::executable_path()|' src/test/system_tests.cpp sed -i '/boost\/test\/\(included\/\)\?unit_test.hpp/d' test/lint/lint-includes.py sed -i 's|#include <test/util/framework.h>|&\n\n#include <memory>|' src/test/result_tests.cpp sed -i '/^ Boost::headers$/d' src/test/kernel/CMakeLists.txt -END VERIFY SCRIPT-



Continued from #35587. tl;dr: Make an equivalent test runner to boost with simpler macros and tighter coupling to bitcoin-specific code.
Motivation
Replacing
Boost.Testas a runner helps us simplify depends and guix as well as use IWYU and clang-tidy within the tests more effectively. It is my opinion that we can also improve the developer experience when writing tests. The repo currently uses a very small subset ofBoost.Test, which offers a relatively simple replacement.Macros
Rant on macros
When using boost, there are 3 ways of writing the same thing, each with different outcomes
BOOST_CHECK(expr1 == expr2): failures show the expression, but no values forexpr1andexpr2BOOST_CHECK_EQUAL(expr1, expr2): failures show the expression and values, but requires developers rememberCHECK_EQUALand does not use usual operandsBOOST_TEST(expr1 == expr2): failures show the expression and values, and developers may use familiar operands (==,!=)All three are preserved to maintain backwards compatibility - presumably, but this results in all 3 being used throughout the repository. As a bonus there is a 4th way, with added context
BOOST_CHECK_MESSAGE(expr1 == expr2, "the context"): failures show the expression and message, but no values forexpr1andexpr2To make debugging and review easier, all of these should be unified to a single macro. Test sites use operands, print helpful messages - values are always included, and do not vary in approach (deciding between
BOOST_TEST,CHECK, andBOOST_CHECK_EQUALis unintuitive)Example boost outputs
New outputs
No repository context
A custom framework allows for complete control of debug output, console output, CLI arguments, etc. #35587 (comment)
Other issues
Issues are mentioned in #34666, #8670, #36045.
High level changes
For those running tests, not a tremendous amount has changed in this PR. For instance, the doc page remains valid. Changes to the runner options include anything that is not
help, run_test, log_level, list_contentor used in CI.For an explanation of all the internal details, I have written a detailed personal blog
Changes in writing tests
Macro shims are added so developers may continue using the usual
BOOST_*macros. Future changes to writing tests would include:CHECKto compare two values with any==,!=,>, etcCHECK(a == b, "my message")to append a messageCHECK_NO_DISPLAYto compare two values without a debug output of said values (expression is printed)REQUIRE, same asCHECK, but will fail the test immediatelyTEST_CASE(name)to add a testFIXTURE_TEST_CASE(name, Fixture)to add a test with a fixtureTEST_SUITE_BEGIN/ENDto declare a suite, optionally with fixture for each test in the suiteMigration
Boost.Testis removed in this PR, but the macros are aliased byBOOST_*counterparts so there are no merge conflicts. The idea would be to migrate test files to the new macros when there are no/low number of conflicts on that file.