ticc_unit_test - (unknown subject)

NAME  SYNOPSIS  DESCRIPTION  List of Functions.  Setting up a testSerie  suppressing output.  Summarizing Tests  EXAMPLES  LIMITATIONS  AUTHORS 

NAME

TiCC Unit Tests - A ’poor mans’ C++ Unit testing Framework

SYNOPSIS

#include ticcutils/UnitTest.h

DESCRIPTION

Ticc Unit Tests provides some powerful functions, combined in a single header file, which give you a simple way to implement Unit Tests.

Ticc Unit Tests aims at easy usage, an quick implementation, of test suites without compiling and linking issues. It is possible to create a test suite by just adding a few assertions around C++ expressions, and Ticc Unit Test will start working at once: counting the number of tests, failures and providing a summary at program end.

Compared to other frameworks, like Boost, CPPUNIT or Google there are some limitations. [But who cares? more to say!]

List of Functions.

assertEqual(expression1,expression2)

test if expression1 equals expression2

If not so: signal the problem and increment the number of detected failures If so: signal success. (except when part of a testSerie. see below)

It is wise to use brackets around the expression, otherwise the compiler may get confused. See Example 1 below.

assertTrue(expression)

test if expression evaluates to true

If not so: signal the problem and increment the number of detected failures If so: signal success. (except when part of a testSerie. see below)

It is wise to use brackets around the expression, otherwise the compiler may get confused. See Example 1 below.

assertMessage(message,expression)

the same as assertTrue() but also displays message on failure.

assertThrow(expression,exception)

test if expression throws the given exception

If not so: signal the problem and increment the number of detected failures If so: signal success. (except when part of a testSerie. see below)

It is wise to use brackets around the expression, otherwise the compiler may get confused. See Example 1 below.

assertNoThrow(expression)

test if expression doesn’t throw.

If not so: signal the problem and increment the number of detected failures If so: signal success. (except when part of a testSerie. see below)

It is wise to use brackets around the expression, otherwise the compiler may get confused. See Example 1 below.

Setting up a testSerie

startTestSerie(message)

Signal the beginning of a series of tests, using message

When a testSerie is active, all success messages of the assert* macro’s are suppressed. Failures are still signaled. At the end of a testSerie a summary is given.

testSerie automatically ends at the end of the program block it is declared in.

suppressing output.

Sometimes it is desirable to suppress excessive success messages, for instance when one of the assert* functions is called in a loop.

This can be achieved with the macro’s:

TEST_SILENT_ON() and TEST_SILENT_OFF()

all failures are still displayed as to inform you off any trouble.

Summarizing Tests

The Ticc Unit test will automatically give a summary of the results at the end of the program. It also returns the number of failures to the shell.

It is however possible to call summarize_tests(expected). The advantage is, that you can provide a number expected to signal that you expected some failures, and that it is not a showstopper.

EXAMPLES

Example 1

#include <string>
#include <iostream>
#include "ticcutils/UnitTest.h"
int main(){
assertEqual( (1+2), (2+1) );
assertTrue( 0 == 1 );
}

The output of this program is:

test: main(6): OK
test: main(7): FAILED
main(7) : ’0’ != TRUE
performed 2 tests, 1 failures. We expected 0 failures.
overall FAILED

When an extra line is added at the end of main():

summarize_tests(1);

The output changes to:

test: main(6): OK
test: main(7): FAILED
main(7) : ’0’ != TRUE
TiCC tests performed 2 tests, with 1 failures. that was what we expected.
overall: OK

Example 2

#include <string>
#include <iostream>
#include <stdexcept>
#include <cmath>
#include "ticcutils/UnitTest.h"

void helper(){
throw std::runtime_error("fout");
}

int main(){
assertThrow( helper(), std::runtime_error );
assertNoThrow( 1/std::sqrt(0) );
}

So we force a runtime error in our helper function, and detect it. We also attempt a division by zero. But on my system, this will result in an inf result and NOT in an exception. Therefor the assertNoThrow() will be satisfied.

The output of this program is:

test: main(13): OK
test: main(14): OK
TiCC tests performed 2 tests, with 0 failures. that was what we expected.
overall: OK

Example 3

So we call a separate function to perform a series of tests.

The output of this program is:

test: main(17): OK

Serie:

test1 (Test some numb stuff)

test1(): all 6 tests OK

test: main(19): OK
TiCC tests performed 8 tests, with 0 failures. that was what we expected.
overall: OK

As you an see, the results of the tests inside the series are summarized. This gives us cleaner output.

LIMITATIONS

There is no overall error detection. So your test program may still fail unexpectedly. For instance on a zero pointer exception or an I/O error.

AUTHORS

Ko van der Sloot [email protected]


Updated 2024-01-29 - jenkler.se | uex.se