A simple and header-only C++ test library for unit-tests.
There are a few ways of using this library. In any case, it is your responsibility to build your test environment.
Just copy the btl.h
to your project.
- Clone the repository into your project (via submodules or whatever)
- In your
CMakeLists.txt
file, add the line:
add_subdirectory("${PROJECT_SOURCE_DIR}/path/to/btl")
target_link_libraries(YourTarget BTL)
DESCRIBE_CLASS(CLASS_TYPE) {}
Describe tests of a class type. Contents must be enclosed in curly braces.DESCRIBE_TEST(CLASS_TYPE) {};
Describe single unit test. Contents must be enclosed in curly braces and must end with semicolon.ASSERT_IS_TRUE(VALUE)
Compare if provided value is true.ASSERT_IS_FALSE(VALUE)
Compare if provided value is false.ASSERT_ARE_EQUAL(VALUE, EXPECTED)
Compare if provided value evaluates the same as the provided expected value.ASSERT_ARE_SAME(VALUE, EXPECTED)
Compare if provided value instance is exactly the same as provided expected instance. Basically, this will cast both&VALUE
and&EXPECTED
to(void*)
and compare their memory addresses.ASSERT_ARRAYS_ARE_EQUAL(VALUE, EXPECTED, RANGE_START, RANGE_LENGTH)
Compare if provided array contains values that evaluate equal to the provided expected array. You ABSOLUTELY MUST provide the range of comparison.
void btl::TestRunner<T>::run()
Run this for each tested class. Successful tests are printed tostdout
. If there are errors, they will be printed tostderr
. The global error count will be updated upon errors as well.bool btl::has_errors()
Check if there were errors in the tests. Return true if there were errors.
// your_type.spec.cxx
#include "your_type.h"
#include <btl.h>
DESCRIBE_CLASS(YourType) {
DESCRIBE_TEST(method_name, CalledMethod, ChangeInstanceToExpectedState) {
YourType foo;
foo.method_name();
ASSERT_ARE_EQUAL(foo.get_state(), YourType::EXPECTED_STATE);
};
DESCRIBE_TEST(method_name, CreatedSituation, ExpectResult) {
// <insert test code here>
};
// <insert other tests here>
}
// test.cxx
#include "your_type.spec.cxx"
#include <btl.h>
int main() {
btl::TestRunner<YourType>::run();
if (btl::has_errors()) {
// Error exit code
return -1;
}
// Success exit code
return 0;
}
If you want to see a real example of some project that uses this, check out Slippy's Math Library.