ticc_unit_test(1)

πŸ“¦ version 0.36, in alpine 3.24, nixos 26.05, nixpkgs unstable

πŸ’– TiCC Unit Tests: ticc_unit_test(1) πŸ’–

okay so bestie, this is ticc_unit_test(1) from the General Commands Manual, and it's literally the TiCC Unit Tests page. No cap, it's a whole vibe.

πŸ’– NAME πŸ’–

TiCC Unit Tests β€” a 'poor mans' C++ Unit testing Framework.

✨ SYNOPSIS ✨

#include ticcutils/UnitTest.h

πŸ¦‹ DESCRIPTION πŸ¦‹

Ticc Unit Tests gives you some powerful functions, all combined in a single header file, so you can implement Unit Tests in a simple way. It's aiming at easy usage and quick implementation of test suites, with no compiling and linking issues.

You can create a test suite by just adding a few assertions around C++ expressions, and Ticc Unit Test starts working at once: it counts the number of tests and failures and provides 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)

tests if expression1 equals expression2.

If not so: signal the problem and increment the number of detected failures. If so: signal success. (Except when it's 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)

tests if expression evaluates to true.

If not so: signal the problem and increment the number of detected failures. If so: signal success. (Except when it's 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)

tests 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 it's 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)

tests if expression doesn't throw.

If not so: signal the problem and increment the number of detected failures. If so: signal success. (Except when it's 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)

Signals 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 can 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 lamasoftware@science.ru.nl


xoxo, your ticc_unit_test(1) bestie πŸ’– β€” from the General Commands Manual, dated 2015 November 26. πŸŽ€βœ¨

πŸ“¦ where this page comes from