|
| 1 | +# RapidCheck property-based testing for Bitcoin Core |
| 2 | + |
| 3 | +## Concept |
| 4 | + |
| 5 | +Property-based testing is experimentally being added to Bitcoin Core with |
| 6 | +[RapidCheck](https://github.com/emil-e/rapidcheck), a C++ framework for |
| 7 | +property-based testing inspired by the Haskell library |
| 8 | +[QuickCheck](https://hackage.haskell.org/package/QuickCheck). |
| 9 | + |
| 10 | +RapidCheck performs random testing of program properties. A specification of the |
| 11 | +program is given in the form of properties which functions should satisfy, and |
| 12 | +RapidCheck tests that the properties hold in a large number of randomly |
| 13 | +generated cases. |
| 14 | + |
| 15 | +If an exception is found, RapidCheck tries to find the smallest case, for some |
| 16 | +definition of smallest, for which the property is still false and displays it as |
| 17 | +a counter-example. For example, if the input is an integer, RapidCheck tries to |
| 18 | +find the smallest integer for which the property is false. |
| 19 | + |
| 20 | +## Running |
| 21 | + |
| 22 | +If RapidCheck is installed, Bitcoin Core will automatically run the |
| 23 | +property-based tests with the unit tests during `make check`, unless the |
| 24 | +`--without-rapidcheck` flag is passed when configuring. |
| 25 | + |
| 26 | +For more information, run `./configure --help` and see `--with-rapidcheck` under |
| 27 | +Optional Packages. |
| 28 | + |
| 29 | +## Setup |
| 30 | + |
| 31 | +The following instructions have been tested with Linux Debian and macOS. |
| 32 | + |
| 33 | +1. Clone the RapidCheck source code and cd into the repository. |
| 34 | + |
| 35 | + ```shell |
| 36 | + git clone https://github.com/emil-e/rapidcheck.git |
| 37 | + cd rapidcheck |
| 38 | + ``` |
| 39 | + |
| 40 | +2. Build RapidCheck (requires CMake to be installed). |
| 41 | + |
| 42 | + ```shell |
| 43 | + cmake -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=true -DRC_ENABLE_BOOST_TEST=ON $(pwd) |
| 44 | + make && make install |
| 45 | + ``` |
| 46 | + |
| 47 | +3. Configure Bitcoin Core with RapidCheck. |
| 48 | + |
| 49 | + `cd` to the directory of your local bitcoin repository and run |
| 50 | + `./configure`. In the output you should see: |
| 51 | + |
| 52 | + ```shell |
| 53 | + checking rapidcheck.h usability... yes |
| 54 | + checking rapidcheck.h presence... yes |
| 55 | + checking for rapidcheck.h... yes |
| 56 | + [...] |
| 57 | + Options used to compile and link: |
| 58 | + [...] |
| 59 | + with test = yes |
| 60 | + with prop = yes |
| 61 | + ``` |
| 62 | + |
| 63 | +4. Build Bitcoin Core with RapidCheck. |
| 64 | + |
| 65 | + Now you can run `make` and should see the property-based tests compiled with |
| 66 | + the unit tests: |
| 67 | + |
| 68 | + ```shell |
| 69 | + Making all in src |
| 70 | + [...] |
| 71 | + CXX test/gen/test_bitcoin-crypto_gen.o |
| 72 | + CXX test/test_bitcoin-key_properties.o |
| 73 | + ``` |
| 74 | + |
| 75 | +5. Run the unit tests with `make check`. The property-based tests will be run |
| 76 | + with the unit tests. |
| 77 | + |
| 78 | + ```shell |
| 79 | + Running tests: crypto_tests from test/crypto_tests.cpp |
| 80 | + [...] |
| 81 | + Running tests: key_properties from test/key_properties.cpp |
| 82 | + ``` |
| 83 | + |
| 84 | +That's it! You are now running property-based tests in Bitcoin Core. |
0 commit comments