|
| 1 | +# *Zero's project* **Test Suite** |
| 2 | + |
| 3 | +## **Introduction** |
| 4 | + |
| 5 | +Welcome to the Zero's project custom test-suite! This test-suite is a lightweight and efficient tool designed to help developers confidently develop their C++ code. With the goal of ensuring code quality and correctness, the test-suite provides a simple yet powerful way to write and run test cases. |
| 6 | + |
| 7 | +This custom test-suite emphasizes flexibility and simplicity, allowing developers to write tests using modern C++ features |
| 8 | +like modules and concepts. It aims to minimize external dependencies and provide a seamless testing experience that integrates |
| 9 | +well with C++ projects. |
| 10 | + |
| 11 | +By using this test-suite, developers can quickly validate their code changes, identify potential issues, and maintain a robust and reliable codebase. |
| 12 | +Whether you are a seasoned C++ developer or just starting your coding journey, the custom test-suite can be a valuable addition to your development toolkit. |
| 13 | + |
| 14 | +### *Disclaimer* |
| 15 | + |
| 16 | +As all the code in `**Zero**`, is a cutting-edge latest C++ standard features, latest standard version |
| 17 | +and latest standard library features codebase. Also, the suite doesn't contains yet a lot of `assertions` |
| 18 | +features, since they will be developed based on our needs. But it would be fine to have a nice collection of |
| 19 | +them, so feel free to open a *PR* and help us! |
| 20 | + |
| 21 | +## **History** |
| 22 | + |
| 23 | +The `tsuite` module born out of the need of having a minimalistic implementation of a test-suite |
| 24 | +that mainly allows ourselves to develop our source code with confidence, without depending on some |
| 25 | +third-party changing standard implementations while avoiding typical dependency pitfalls. |
| 26 | + |
| 27 | +We used for a long time the `Catch2` test suite, but, since the release of `Clang 16` our usage of |
| 28 | +`C++23` and modules made `Clang` go crazy when trying to compile `Catch 2`, so we lost our ability to |
| 29 | +develop this codebase based on *TDD*, and lost basically our testing workflow. |
| 30 | + |
| 31 | +Other alternatives that we've tried does not fit particularly well with our workflow. And, since we try |
| 32 | +to use the latest standard releases, with the latest **std** library features implemented in our used |
| 33 | +compilers, we've always ended losing more time trying to understand what's not ready yet in the compilers, |
| 34 | +or how what we should be doing to make it work. |
| 35 | + |
| 36 | +Even tho, we doesn't mean that there's no good C++ test suites out there. In fact, there's a couple of |
| 37 | +good really ones, with years of experience, like `Catch2` and `GTest`. But, since we also do not use any of |
| 38 | +the mainstream build systems (we're using **Zork++**), those suites are highly integrated with them, but not |
| 39 | +ready to use out-of-the-box within our development environment, so finally, we decided that we could spend |
| 40 | +some time writing a whole new suite that better fits our environment's needs, that is ready to work with modules |
| 41 | +without any complication, and that will be growing up and scaling based on user's feedback and our own needs. |
| 42 | + |
| 43 | +## **Test Suite Structure** |
| 44 | +The custom test-suite consists of two modules: the `tsuite` primary interface module and `tsuite:assertions` module partition. |
| 45 | +The `tsuite` module handles test suite registration, test case registration, and test execution. |
| 46 | +On the other hand, the `tsuite:assertions` partition contains various assertion functions used within the test cases. |
| 47 | + |
| 48 | +Notice that `tsuite:assertions` is a partition module interface related with `tsuite`. You only need to |
| 49 | +`import tsuite` to be able to work with all the features available within the suite, as they are all |
| 50 | +re-exported through the primary module interface `tsuite`. |
| 51 | + |
| 52 | +With this organized structure, developers can easily register test cases and suites, run tests, and obtain meaningful feedback on the results. The suite provides a clear separation of concerns, enabling smooth collaboration between different team members and enhancing code maintainability. |
| 53 | + |
| 54 | +## **Standalone Tests or Test Suites** |
| 55 | + |
| 56 | +#### - **Standalone Tests**: |
| 57 | +Standalone tests are individual test cases that are registered directly in the test suite using the `TEST_CASE(...)` function. |
| 58 | +These tests can be defined as standalone functions or lambdas and are registered with a unique name that identifies the test case. |
| 59 | +Standalone tests are suitable for small, isolated test scenarios that don't need to be grouped together. |
| 60 | + |
| 61 | +#### - **Test Suites**: |
| 62 | +Test suites are groups of related test cases that are logically organized together. |
| 63 | +Each test suite has a unique identifier called uuid, and it contains multiple test cases. |
| 64 | +Test cases within a suite can be registered using the `TEST_CASE(...)` function, |
| 65 | +just like standalone tests, but choosing the overload that receives as its first parameter |
| 66 | +a reference to the test suite. |
| 67 | + |
| 68 | +## **Example of usage** |
| 69 | + |
| 70 | +Here's an example of how to use the custom test-suite to write and run test cases: |
| 71 | + |
| 72 | +```c++ |
| 73 | +// Import the necessary modules |
| 74 | +import std; // Should be ready on all the major compilers for C++23. But until this date, any |
| 75 | +// one of them made the std lib implementation as a module as the standard mandates, so we are working |
| 76 | +// with the `Zork++` out of the box solution based on Clang modulemaps. |
| 77 | +import tsuite; |
| 78 | + |
| 79 | +// Define test functions. Should be void functions that later w'd be registered in a suite. |
| 80 | +void testAddition() { |
| 81 | + int result = 2 + 2; |
| 82 | + assertEquals(4, result); |
| 83 | +} |
| 84 | + |
| 85 | +// Passing two pointers to compare if the values that they point to are equals |
| 86 | +void testPtrsAddition() { |
| 87 | + int result = 2 + 2; |
| 88 | + int expected = 4; |
| 89 | + assertEquals(&expected, &result); |
| 90 | +} |
| 91 | + |
| 92 | +// Driver code |
| 93 | +int main() { |
| 94 | + // Free tests cases registration examples |
| 95 | + |
| 96 | + // Register a new test case using a function pointer. |
| 97 | + TEST_CASE("Addition Test With Pointers", testPtrsAddition); |
| 98 | + |
| 99 | + // Users can register a new test case using lambdas, avoiding to write standalone functions |
| 100 | + TEST_CASE("Subtraction Test", []() { |
| 101 | + int result = 5 - 3; |
| 102 | + assertEquals(122435, result); |
| 103 | + }); |
| 104 | + |
| 105 | + // Registering test cases into test suites, to group and relate tests that makes sense to exists |
| 106 | + // as a part of a whole |
| 107 | + |
| 108 | + // Instantiate a new test suite, giving it a unique identifier. |
| 109 | + TestSuite suite {"My Suite"}; |
| 110 | + // Register test cases using function pointers into a test suite |
| 111 | + TEST_CASE(suite, "Addition Test", testAddition); |
| 112 | + // Force a warning that alerts the user that the test will be discarded, since already |
| 113 | + // exists one with the same identifier in the given suite |
| 114 | + TEST_CASE(suite, "Addition Test", testAddition); |
| 115 | + |
| 116 | + // Don't forget to call this free function, to run all the tests written! |
| 117 | + RUN_TESTS(); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +With these example, you will see this result: |
| 124 | + |
| 125 | + |
| 126 | +## Funny facts |
| 127 | + |
| 128 | +As you see in the examples, we mostly use upper snake case convention for the standalone functions |
| 129 | +that takes care of register tests or run all the test written. But... they are not macros! |
| 130 | +They are just regular standalone functions available within the `zero` namespace. |
| 131 | + |
| 132 | +One of the worst part of the `C++` test suites is that almost every one are macro-based. |
| 133 | +But, for having a kind of environmental integration, and as a kind of tribute as well, |
| 134 | +as we've maintained the name of these free functions with the same conventions used for |
| 135 | +most of them. |
0 commit comments