|
| 1 | +# C++ testing guide |
| 2 | + |
| 3 | +C++ tests use a modified version of the [Tut test framework](https://mrzechonek.github.io/tut-framework/). |
| 4 | + |
| 5 | +Test files are placed in test/cxx/SomethingTest.cpp. |
| 6 | + |
| 7 | +## Test suite example |
| 8 | + |
| 9 | +```c++ |
| 10 | +#include <TestSupport.h> // Always include this |
| 11 | +#include <Something.h> // The class to test |
| 12 | + |
| 13 | +// Always use these namespaces |
| 14 | +using namespace std; |
| 15 | +using namespace boost; |
| 16 | +using namespace Passenger; |
| 17 | + |
| 18 | +namespace tut { |
| 19 | + // Each test suite has a corresponding context struct, created and |
| 20 | + // deleted for every test case. You can put test case-local state here. |
| 21 | + struct SomethingTest: public TestBase { |
| 22 | + Something something; |
| 23 | + }; |
| 24 | + |
| 25 | + TEST_METHOD(1) { |
| 26 | + set_test_name("Description for test 1"); |
| 27 | + |
| 28 | + // Test logic here. |
| 29 | + // `this` is a SomethingTest instance. |
| 30 | + |
| 31 | + ensure_equals("Description for assertion 1", 1, something.foo()); |
| 32 | + ensure_equals("Description for assertion 2", 2, something.bar()); |
| 33 | + } |
| 34 | + |
| 35 | + TEST_METHOD(2) { |
| 36 | + set_test_name("Description for test 2"); |
| 37 | + |
| 38 | + // Test logic here. |
| 39 | + // `this` is a SomethingTest instance. |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | +
|
| 44 | +## Available assertions |
| 45 | +
|
| 46 | + - `ensure([description,] bool)` — Asserts argument is true. |
| 47 | + - `ensure_not([description,] bool)` |
| 48 | + - `ensure_equals([description,] actual, expected)` — Asserts `actual == expected`. |
| 49 | + - `ensure_not_equals([description,] actual, expected)` |
| 50 | + - `ensure_gt(description, a, b)` — Asserts `a > b`. |
| 51 | + - `ensure_distance([description,] a, b, d)` — Asserts the distance between `a` and `b` is <= `d`. |
| 52 | + - `fail(description)` — Fails the test case. |
| 53 | +
|
| 54 | +### Special assertions for multithreaded code |
| 55 | +
|
| 56 | + - `EVENTUALLY(deadlineSeconds, code)` — Asserts that "something eventually happens". |
| 57 | +
|
| 58 | + Runs `code` in a loop, sleeping for 10 msec each time, until code set `result = true` or timeout. |
| 59 | +
|
| 60 | + Example: |
| 61 | +
|
| 62 | + ```c++ |
| 63 | + EVENTUALLY(5, |
| 64 | + result = fileExists("foo.txt"); |
| 65 | + ); |
| 66 | + ``` |
| 67 | + |
| 68 | + Notes: |
| 69 | + |
| 70 | + - `code` is in a new lexical context, so defining variables there is fine. |
| 71 | + - Since EVENTUALLY is for multithreaded use cases, remember to synchronize access to shared state. |
| 72 | + |
| 73 | +- `EVENTUALLY2(deadlineMsec, sleeptimeMsec, code)` — Same as EVENTUALLY but with finer timing customization. |
| 74 | + |
| 75 | +- `SHOULD_NEVER_HAPPEN(deadlineMsec, code)` — Asserts that "something should never happen". |
| 76 | + |
| 77 | + Runs `code` in a loop for `deadlineMsec`. If `code` sets `result = true`, the test fails. |
| 78 | + |
| 79 | + Example: |
| 80 | + |
| 81 | + ```c++ |
| 82 | + SHOULD_NEVER_HAPPEN(1000, |
| 83 | + result = checkSocketDisconnected(); |
| 84 | + ); |
| 85 | + ``` |
| 86 | +
|
| 87 | + The notes for `EVENTUALLY()` also apply here. |
| 88 | +
|
| 89 | +## Mocking |
| 90 | +
|
| 91 | +See [C++ mocking strategy](CxxMockingStrategy.md). |
| 92 | +
|
| 93 | +## Running tests |
| 94 | +
|
| 95 | +Prerequisite: ensure `test/config.json` exists. Refer to its `.example` file. |
| 96 | +
|
| 97 | +```bash |
| 98 | +# Run all test suites |
| 99 | +rake test:cxx GROUPS=SomethingTest |
| 100 | +
|
| 101 | +# Run specific test suites |
| 102 | +rake test:cxx GROUPS=SomethingTest,AnotherTest |
| 103 | +
|
| 104 | +# Run specific tests by number |
| 105 | +rake test:cxx GROUPS=SomethingTest:1,3 |
| 106 | +
|
| 107 | +# Attach to GDB or LLDB |
| 108 | +rake test:cxx GROUPS=SomethingTest GDB=1 |
| 109 | +rake test:cxx GROUPS=SomethingTest LLDB=1 |
| 110 | +``` |
0 commit comments