CAT is a lightweight and simple framework for writing tests in C. CAT can be interpreted as C Awesome Tests, C Automated Tests, cat or whatever you like.
- Linux
- Mac OS X not supported
- Windows not supported
CAT_CASE(test_case_name)
{
...
}
CAT_CASE(test_case_name, test_suite_name)
CAT_SKIP(expression)
Skips the test if evaluates expression.
CAT_ASSERT(expression)
Asserts that condition is true. Otherwise the test case will fail
CAT_ASSERT_NULL(ptr)
Asserts that ptr is NULL
CAT_ASSERT_NOT_NULL(ptr)
Asserts that ptr is not NULL
CAT_ASSERT_STR_EQUAL(actual, expected)
Asserts that strings actual equals to expected
CAT_ASSERT_STR_NOT_EQUAL(str1, str2)
Compares str1 with str2. Passes when they are not equal. Otherwise the test will fail.
CAT_BEFORE_EACH()
{
...
}
Function to be executed before each CAT_CASE
.
CAT_AFTER_EACH()
{
...
}
Function to be executed after each CAT_CASE
.
CAT_FUNC(name, args)
{
...
}
Declares a test function which name is the first argument name. Function will pass if all expressions inside will be passed. Otherwise, it will fail and the test case will return failure.
CAT_FUNC_RUN(name, args)
Calls a test function by name
CAT_MOCK(original, mocked_func)
Declare mock behavior for an external resource.
CAT_UNMOCK(original)
Restores mocked behaviour to original.
static void *
mallock_mock(size_t size)
{
(void)size;
return NULL;
}
CAT_CASE(sample1)
{
void *ptr = NULL;
CAT_MOCK(malloc, mallock_mock);
ptr = malloc(32);
CAT_ASSERT_NULL(ptr);
CAT_UNMOCK(malloc);
ptr = malloc(32);
CAT_ASSERT_NOT_NULL(ptr);
free(ptr);
}
Just create a sample test source file test.c
#include "cat.h"
CAT_CASE(test)
{
CAT_ASSERT(1);
}
Then make a executable test using gcc
gcc -I./src test.c ./src/cat.c -o test
Check if it is working
./test -a
Running test cases
- [PASS] test
Summary
TOTAL PASSED FAILED SKIPPED UNKNOWN
1 1 0 0 0
Time elapsed: 0.000127 seconds
==================== SUCCESS ====================
Usage: ./test [OPTIONS] [TEST, ...]
Options:
-h Prints this help
-l Lists all declared unit tests
Run options:
-a Run all tests
-p Run tests in single process
-s [SUITE, ...] Run test suites
For detailed information and usage see samples in examples
directory.
#include "cat.h"
CAT_CASE(test1)
{
CAT_ASSERT(1);
}
CAT_CASE(test2)
{
void *ptr = malloc(1);
CAT_ASSERT_NOT_NULL(ptr);
}
Test functions can be a good friend in order to avoid duplicated source code in each test case. If a test function will fail - the unit test will be failed also.
#include "cat.h"
CAT_FUNC(compare, int a, int b)
{
CAT_ASSERT(a == b);
}
CAT_CASE(test1)
{
CAT_FUNC_RUN(compare, 1, 2);
}