Automating Tests in C++
Recently I have been doing a lot of C++ programming. Some of the projects I was and currently am working on include:
- PAG (and XY), a top-down operator precedence parser (TDOP) and front-end for a potential programming language.
- C++ PEG library with full support for left recursion that uses the leftmost derivation when deriving left recursive rules.
- The Formal Language Template Library (FLTL) that is the foundation of my undergraduate thesis project.
Through these projects, I've exposed myself to more C++ and I am finding that I (gasp!) like using it in some of my programming activities. That is not to say that C++ is my tool of choice for all projects, but for the above three it certainly was.
Most recently, I have been working on the FLTL and part of the requirements of the FLTL is that it work on a number of platforms. There are usually two main problems that come up when I try to make this work:
- Compiler errors.
- Unexpected runtime errors.
To start things off, the following code illustrates how I use the system.
As is shown above, test cases are contained within functions. A function can contain as many tests cases as one likes. The idea is that the function represents a category of test cases. Then, to add a category of test cases to the system, one simply declares (in a header file) that a function is a test category (using the FLTL_TEST_CATEGORY macro).
The way this works is fairly straightforward:
- Test cases are basically assertions. If the condition of the test case is true then the test succeeds, otherwise the test fails. Successes and failures are recorded and printed out.
- Each test category declaration declares the function and also a static variable whose type is parameterized over the function. When static variable constructors are called at runtime, the test case category types chain themselves into a linked list.
- When we want to run the tests, we invoke fltl::test::run_tests() and that loops through the linked list and invokes each test case category function.
The code that includes the testing macros and types can be found here:
Comments
Comment