Skip to content
/ cat Public

CAT is a lightweight and simple framework for writing tests in C

License

Notifications You must be signed in to change notification settings

Talonas/cat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CAT

CAT Build Status

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.

Platforms

  • Linux
  • Mac OS X not supported
  • Windows not supported

Test Case

CAT_CASE(test_case_name)
{
	...
}

Test Suites

CAT_CASE(test_case_name, test_suite_name)

Test Skip

CAT_SKIP(expression)

Skips the test if evaluates expression.

Assertions

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.

Before/After Each

CAT_BEFORE_EACH()
{
	...
}

Function to be executed before each CAT_CASE.

CAT_AFTER_EACH()
{
	...
}

Function to be executed after each CAT_CASE.

Functions

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

Mocking

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);
}

Usage

Build

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 ====================

Run Options

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

Samples

Examples

For detailed information and usage see samples in examples directory.

Basic

#include "cat.h"

CAT_CASE(test1)
{
	CAT_ASSERT(1);
}

CAT_CASE(test2)
{
	void *ptr = malloc(1);
	CAT_ASSERT_NOT_NULL(ptr);
}

Using Test Functions

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);
}

About

CAT is a lightweight and simple framework for writing tests in C

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages