Library meant to simplify writing of tests. Very light-weight in the approach. Test syntax will be familiar to users of CUnit, however this is far more basic.
Dependents: CircularBufferTest
Tests are expected to take the form of macro-based statements, such as:
// An empty buffer should yield getSize() of zero TST_EQ( buffer.getSize(), 0, "Empty buffer: getSize()" );
You still have to do the hard work of coming up with the tests and writing code to implement them, but this library is intended to give you a "jump start" towards getting something with consistent output and keeping track of the number of passes/fails - just import the library, include the header and you're away.
See CircularBufferTest main.cpp for an example of how this library can be used
TestSupportLite.hpp@0:62a10b8392a4, 2014-01-19 (annotated)
- Committer:
- johnb
- Date:
- Sun Jan 19 17:14:59 2014 +0000
- Revision:
- 0:62a10b8392a4
Quick utility library to support testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnb | 0:62a10b8392a4 | 1 | /** |
johnb | 0:62a10b8392a4 | 2 | @file |
johnb | 0:62a10b8392a4 | 3 | @brief Support routines for writing very simple test harnesses |
johnb | 0:62a10b8392a4 | 4 | |
johnb | 0:62a10b8392a4 | 5 | @author John Bailey |
johnb | 0:62a10b8392a4 | 6 | |
johnb | 0:62a10b8392a4 | 7 | @copyright Copyright 2014 John Bailey |
johnb | 0:62a10b8392a4 | 8 | |
johnb | 0:62a10b8392a4 | 9 | @section LICENSE |
johnb | 0:62a10b8392a4 | 10 | |
johnb | 0:62a10b8392a4 | 11 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 0:62a10b8392a4 | 12 | you may not use this file except in compliance with the License. |
johnb | 0:62a10b8392a4 | 13 | You may obtain a copy of the License at |
johnb | 0:62a10b8392a4 | 14 | |
johnb | 0:62a10b8392a4 | 15 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 0:62a10b8392a4 | 16 | |
johnb | 0:62a10b8392a4 | 17 | Unless required by applicable law or agreed to in writing, software |
johnb | 0:62a10b8392a4 | 18 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 0:62a10b8392a4 | 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 0:62a10b8392a4 | 20 | See the License for the specific language governing permissions and |
johnb | 0:62a10b8392a4 | 21 | limitations under the License. |
johnb | 0:62a10b8392a4 | 22 | |
johnb | 0:62a10b8392a4 | 23 | */ |
johnb | 0:62a10b8392a4 | 24 | |
johnb | 0:62a10b8392a4 | 25 | #if !defined TESTSUPPORTLITE_HPP |
johnb | 0:62a10b8392a4 | 26 | #define TESTSUPPORTLITE_HPP |
johnb | 0:62a10b8392a4 | 27 | |
johnb | 0:62a10b8392a4 | 28 | #include <cstring> // for size_t |
johnb | 0:62a10b8392a4 | 29 | |
johnb | 0:62a10b8392a4 | 30 | /** Boiler plate test support for the TST_EQ, TST_TRUE, etc macros |
johnb | 0:62a10b8392a4 | 31 | \param _p Indicate whether or not the test passed |
johnb | 0:62a10b8392a4 | 32 | \param _t Description of the test |
johnb | 0:62a10b8392a4 | 33 | */ |
johnb | 0:62a10b8392a4 | 34 | #define TST_BOILER( _p, _t) do { TEST_OUT( _p, _t ); if( !_p ) { FAIL_OUT(); } } while ( 0 ) |
johnb | 0:62a10b8392a4 | 35 | |
johnb | 0:62a10b8392a4 | 36 | /** Testing macro - expects _x and _q to be equal (using the equality operator) |
johnb | 0:62a10b8392a4 | 37 | \param _x Value to be compared |
johnb | 0:62a10b8392a4 | 38 | \param _y Value to be compared |
johnb | 0:62a10b8392a4 | 39 | \param _t Description of test |
johnb | 0:62a10b8392a4 | 40 | */ |
johnb | 0:62a10b8392a4 | 41 | #define TST_EQ( _x, _y, _t ) do { bool passed = ((_x)==(_y)); TST_BOILER( passed, _t ); } while( 0 ) |
johnb | 0:62a10b8392a4 | 42 | |
johnb | 0:62a10b8392a4 | 43 | /** Testing macro - expects _x to be true |
johnb | 0:62a10b8392a4 | 44 | \param _x Value to be evaluated |
johnb | 0:62a10b8392a4 | 45 | \param _t Description of test |
johnb | 0:62a10b8392a4 | 46 | */ |
johnb | 0:62a10b8392a4 | 47 | #define TST_TRUE( _x, _t ) do { bool passed = (_x); TST_BOILER( passed, _t ); } while( 0 ) |
johnb | 0:62a10b8392a4 | 48 | |
johnb | 0:62a10b8392a4 | 49 | /** Testing macro - expects _x to be false |
johnb | 0:62a10b8392a4 | 50 | \param _x Value to be evaluated |
johnb | 0:62a10b8392a4 | 51 | \param _t Description of test |
johnb | 0:62a10b8392a4 | 52 | */ |
johnb | 0:62a10b8392a4 | 53 | #define TST_FALSE( _x, _t ) TST_TRUE( !( _x ), _t ) |
johnb | 0:62a10b8392a4 | 54 | |
johnb | 0:62a10b8392a4 | 55 | /** Tasks to be carried out when the tests are over */ |
johnb | 0:62a10b8392a4 | 56 | #define TST_DONE() do { DONE_OUT(); } while( 0 ) |
johnb | 0:62a10b8392a4 | 57 | |
johnb | 0:62a10b8392a4 | 58 | /** Currently a very simple class just to encapsulate a few test statistics |
johnb | 0:62a10b8392a4 | 59 | |
johnb | 0:62a10b8392a4 | 60 | Implemented as a class rather than a set of global variables to simplify |
johnb | 0:62a10b8392a4 | 61 | multiple instatitation |
johnb | 0:62a10b8392a4 | 62 | */ |
johnb | 0:62a10b8392a4 | 63 | class TestResults |
johnb | 0:62a10b8392a4 | 64 | { |
johnb | 0:62a10b8392a4 | 65 | protected: |
johnb | 0:62a10b8392a4 | 66 | /** Number of tests that have passed */ |
johnb | 0:62a10b8392a4 | 67 | std::size_t m_passed; |
johnb | 0:62a10b8392a4 | 68 | /** Total number of tests run so far */ |
johnb | 0:62a10b8392a4 | 69 | std::size_t m_total; |
johnb | 0:62a10b8392a4 | 70 | public: |
johnb | 0:62a10b8392a4 | 71 | /* Default initialiser */ |
johnb | 0:62a10b8392a4 | 72 | TestResults( void ); |
johnb | 0:62a10b8392a4 | 73 | |
johnb | 0:62a10b8392a4 | 74 | /* Register that a test has been run |
johnb | 0:62a10b8392a4 | 75 | \parap p_passed Whether or not the test passed successfully */ |
johnb | 0:62a10b8392a4 | 76 | void registerResult( const bool p_passed ); |
johnb | 0:62a10b8392a4 | 77 | |
johnb | 0:62a10b8392a4 | 78 | /* Query the total number of tests run so far */ |
johnb | 0:62a10b8392a4 | 79 | std::size_t getCount( void ) const; |
johnb | 0:62a10b8392a4 | 80 | |
johnb | 0:62a10b8392a4 | 81 | /* Query the number of tests passed so far */ |
johnb | 0:62a10b8392a4 | 82 | std::size_t getPassed( void ) const; |
johnb | 0:62a10b8392a4 | 83 | }; |
johnb | 0:62a10b8392a4 | 84 | |
johnb | 0:62a10b8392a4 | 85 | #endif // !defined TESTSUPPORTLITE_HPP |