Skip to content

Commit 9edb2b6

Browse files
committed
Merge #16953: doc: Improve test READMEs
43e7d57 doc: Improve test READMEs (Fabian Jahr) Pull request description: General improvements on READMEs for unit tests and functional tests: - Give unit test readme a headline - Move general information on `src/test` folder to the top - Add information on logging and debugging unit tests - Improve debugging and logging information in functional testing - Include all available log levels in functional tests ACKs for top commit: laanwj: ACK 43e7d57 Tree-SHA512: 22b27644992ba5d99a885cd51b7a474806714396fcea1fd2d6285e41bdf3b28835ad8c81449099e3ee15a63d57b3ab9acb89c425d9855ed1d9b4af21db35ab03
2 parents 6b2210f + 43e7d57 commit 9edb2b6

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

src/test/README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# Unit tests
2+
3+
The sources in this directory are unit test cases. Boost includes a
4+
unit testing framework, and since Bitcoin Core already uses Boost, it makes
5+
sense to simply use this framework rather than require developers to
6+
configure some other framework (we want as few impediments to creating
7+
unit tests as possible).
8+
9+
The build system is set up to compile an executable called `test_bitcoin`
10+
that runs all of the unit tests. The main source file is called
11+
`setup_common.cpp`.
12+
113
### Compiling/running unit tests
214

315
Unit tests will be automatically compiled if dependencies were met in `./configure`
@@ -12,7 +24,7 @@ to run the bitcoind tests.
1224

1325
To add more bitcoind tests, add `BOOST_AUTO_TEST_CASE` functions to the existing
1426
.cpp files in the `test/` directory or add new .cpp files that
15-
implement new BOOST_AUTO_TEST_SUITE sections.
27+
implement new `BOOST_AUTO_TEST_SUITE` sections.
1628

1729
To run the bitcoin-qt tests manually, launch `src/qt/test/test_bitcoin-qt`
1830

@@ -32,20 +44,24 @@ example, to run just the getarg_tests verbosely:
3244

3345
Run `test_bitcoin --help` for the full list.
3446

35-
### Note on adding test cases
36-
37-
The sources in this directory are unit test cases. Boost includes a
38-
unit testing framework, and since bitcoin already uses boost, it makes
39-
sense to simply use this framework rather than require developers to
40-
configure some other framework (we want as few impediments to creating
41-
unit tests as possible).
47+
### Adding test cases
4248

43-
The build system is setup to compile an executable called `test_bitcoin`
44-
that runs all of the unit tests. The main source file is called
45-
setup_common.cpp. To add a new unit test file to our test suite you need
49+
To add a new unit test file to our test suite you need
4650
to add the file to `src/Makefile.test.include`. The pattern is to create
4751
one test file for each class or source file for which you want to create
48-
unit tests. The file naming convention is `<source_filename>_tests.cpp`
52+
unit tests. The file naming convention is `<source_filename>_tests.cpp`
4953
and such files should wrap their tests in a test suite
5054
called `<source_filename>_tests`. For an example of this pattern,
51-
examine `uint256_tests.cpp`.
55+
see `uint256_tests.cpp`.
56+
57+
### Logging and debugging in unit tests
58+
59+
To write to logs from unit tests you need to use specific message methods
60+
provided by Boost. The simplest is `BOOST_TEST_MESSAGE`.
61+
62+
For debugging you can launch the test_bitcoin executable with `gdb`or `lldb` and
63+
start debugging, just like you would with bitcoind:
64+
65+
```bash
66+
gdb src/test/test_bitcoin
67+
```

test/README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ killall bitcoind
136136

137137
##### Test logging
138138

139-
The tests contain logging at different levels (debug, info, warning, etc). By
140-
default:
139+
The tests contain logging at five different levels (DEBUG, INFO, WARNING, ERROR
140+
and CRITICAL). From within your functional tests you can log to these different
141+
levels using the logger included in the test_framework, e.g.
142+
`self.log.debug(object)`. By default:
141143

142144
- when run through the test_runner harness, *all* logs are written to
143145
`test_framework.log` and no logs are output to the console.
@@ -182,18 +184,32 @@ call methods that interact with the bitcoind nodes-under-test.
182184
If further introspection of the bitcoind instances themselves becomes
183185
necessary, this can be accomplished by first setting a pdb breakpoint
184186
at an appropriate location, running the test to that point, then using
185-
`gdb` to attach to the process and debug.
187+
`gdb` (or `lldb` on macOS) to attach to the process and debug.
186188

187-
For instance, to attach to `self.node[1]` during a run:
189+
For instance, to attach to `self.node[1]` during a run you can get
190+
the pid of the node within `pdb`.
191+
192+
```
193+
(pdb) self.node[1].process.pid
194+
```
195+
196+
Alternatively, you can find the pid by inspecting the temp folder for the specific test
197+
you are running. The path to that folder is printed at the beginning of every
198+
test run:
188199

189200
```bash
190201
2017-06-27 14:13:56.686000 TestFramework (INFO): Initializing test directory /tmp/user/1000/testo9vsdjo3
191202
```
192203

193-
use the directory path to get the pid from the pid file:
204+
Use the path to find the pid file in the temp folder:
194205

195206
```bash
196207
cat /tmp/user/1000/testo9vsdjo3/node1/regtest/bitcoind.pid
208+
```
209+
210+
Then you can use the pid to start `gdb`:
211+
212+
```bash
197213
gdb /home/example/bitcoind <pid>
198214
```
199215

0 commit comments

Comments
 (0)