Skip to content

nilobject/DealberttCTest

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📘 CTest — C Unit Testing Library Inspired by JUnit

A lightweight unit testing framework for C inspired by JUnit, supporting grouped tests, assertions, source tracking, and formatted output.


Table of Contents


Getting Started

First, build the library on the root folder of the repo using the next command:

make

Once the library has been build, in order to install it in your system you can do the next command:

sudo make install

If you want to completely remove the library from your system, just execute the follwing command:

sudo make uninstall

Include ctest.h in your project and link against the implementation file (not shown here).

#include "ctest.h"

Create tests as functions, group them, and run the suite.

When compiling, make sure to include the following flag:

gcc main.c -o whatevername -lctest

Test Groups & Running Tests

int initGroup(TestGroup *group);
int addTest(TestGroup *group, const char *name, CTest *test);
int runTests(TestGroup *group);

Example

void sampleTest(TestResult *res) {
    assertEqualsInt(5, 2 + 3);
}

int main() {
    TestGroup group;
    initGroup(&group);

    CTest test = { .func = sampleTest };
    addTest(&group, "Addition Test", &test);

    runTests(&group);
    return 0;
}

Assertions

All assertions track the source file, line number, and function using __FILE__, __LINE__, and __func__ via the CUR_SOURCE_LOCATION macro.


Equality Assertions

assertEqualsInt(expected, actual);
assertEqualsShort(expected, actual);
assertEqualsLong(expected, actual);
assertEqualsFloat(expected, actual);
assertEqualsDelta(expected, actual, delta);
assertEqualsDouble(expected, actual);
assertEqualsChar(expected, actual);
assertEqualsStr(expected, actual);

Array Versions

assertEqualsArrayInt(expected, actual);
assertEqualsArrayShort(expected, actual);
assertEqualsArrayLong(expected, actual);
assertEqualsArrayFloat(expected, actual);
assertEqualsArrayDouble(expected, actual);

Inequality Assertions

assertNotEqualsInt(expected, actual);
assertNotEqualsShort(expected, actual);
assertNotEqualsLong(expected, actual);
assertNotEqualsFloat(expected, actual);
assertNotEqualsDouble(expected, actual);
assertNotEqualsChar(expected, actual);
assertNotEqualsStr(expected, actual);

Array Versions

assertNotEqualsArrayInt(expected, actual);
assertNotEqualsArrayShort(expected, actual);
assertNotEqualsArrayLong(expected, actual);
assertNotEqualsArrayFloat(expected, actual);
assertNotEqualsArrayDouble(expected, actual);

Boolean Assertions

assertTrue(actual);
assertFalse(actual);

Null Assertions

assertNotNull(pointer);
assertNull(pointer);

Configuration

You can enable extra debug and verbosity at compile-time using:

#define DEBUG_ASSERT_ENABLED 1
#define VERBOSE_ASSERT_RESULT 1

When enabled, DEBUG_ASSERT() will track and display failed expressions with source metadata.


Colors and Output

The framework uses ANSI color codes:

  • red() – Failures
  • green() – Successes
  • yellow(), blue(), white() – Neutral/informational
  • clear() – Clears terminal output

These macros can be used in user code for custom formatting as well.


License

Include your license here if applicable.

About

A unit test library written in C by Dealbertt

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 97.5%
  • Makefile 2.5%