|
| 1 | +# library-with-tests |
| 2 | + |
| 3 | +A single Cabin package with a `library` target and two `test` targets |
| 4 | +that exercise it. This is the example to read for `cabin test`. |
| 5 | + |
| 6 | +A Cabin `test` target is an ordinary executable: it **passes when its |
| 7 | +`main()` returns `0`** and fails otherwise. There is no framework, |
| 8 | +macro, or attribute to learn. `cabin test` builds every `type = "test"` |
| 9 | +target, runs each one, and reports a per-target `... ok` / `... FAILED` |
| 10 | +line plus a summary. Tests run in a deterministic order — by package |
| 11 | +name, then target name — so `calc_test` always runs before |
| 12 | +`parity_test`. |
| 13 | + |
| 14 | +Both tests depend on the `calc` library through `deps = ["calc"]` and |
| 15 | +include its public header through `calc`'s `include_dirs = ["include"]`. |
| 16 | + |
| 17 | +## Run the tests |
| 18 | + |
| 19 | +```sh |
| 20 | +cd examples/library-with-tests |
| 21 | +cabin test |
| 22 | +``` |
| 23 | + |
| 24 | +Expected output (the `Compiling …` build line goes to stderr and is |
| 25 | +omitted here): |
| 26 | + |
| 27 | +``` |
| 28 | +running 2 tests |
| 29 | +running test library-with-tests:calc_test |
| 30 | +running test library-with-tests:parity_test |
| 31 | +test library-with-tests:calc_test ... ok |
| 32 | +test library-with-tests:parity_test ... ok |
| 33 | +test result: ok. 2 passed; 0 failed (of 2) |
| 34 | +``` |
| 35 | + |
| 36 | +A passing test is silent on stdout. The `check(...)` helper in |
| 37 | +`tests/*.cc` only writes (to stderr) when an assertion fails, which |
| 38 | +also makes that target exit non-zero so `cabin test` reports it as |
| 39 | +`FAILED (exit N)` and the command exits non-zero. |
| 40 | + |
| 41 | +This package has no `executable` target, so `cabin run` does not apply |
| 42 | +here. A plain `cabin build` compiles only the `calc` library; the |
| 43 | +`test` targets are dev-only, so `cabin test` is what builds *and* runs |
| 44 | +the two test binaries. |
0 commit comments