/carbon

A simple, lightweight, straightforward C/C++ unit testing framework written in pure C. 🧪

Primary LanguageCBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Carbon Logo
Carbon

You can’t unit test C, right?

Buy Me A Coffee

License C Standard Size Release Blazing Speed

CI Test

(…)

Carbon is free software: you can redistribute it and/or modify it under the terms of the BSD 3-Clause “New” or “Revised” License as published by The Regents of the University of California.
Carbon is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD 3-Clause “New” or “Revised” License for more details.
You should have received a copy of the BSD 3-Clause “New” or “Revised” License along with Carbon. If not, see https://opensource.org/license/BSD-3-Clause.

Table of Contents

Usage

(…)

$  git submodule add https://github.com/sparky-game/carbon vendor/carbon

(…)

Target code

(…)

// include/x.h

#pragma once

void inc_int(int *x);
// src/x.c

#include <x.h>

void inc_int(int *x) {
  ++(*x);
}

(…)

gcc -I include --coverage -c src/x.c -o build/x.o

Test code

(…)

// test/include/x_test.h

#pragma once

void x_test_register(void);
// test/src/x_test.c

#include <x.h>
#include <x_test.h>
#include <carbon.h>

static unsigned char x_test_inc_int(void) {
  int a = 1, b = 0;
  carbon_should_not_be(a, b);
  inc_int(&b);
  carbon_should_be(a, b);
  return 1;
}

void x_test_register(void) {
  CARBON_REGISTER_TEST(x_test_inc_int);
}

(…)

gcc -I include -I test/include -I vendor/carbon -c test/src/x_test.c -o build/test/x_test.o

Test suite entrypoint

(…)

// test/src/carbon.c

#define CARBON_IMPLEMENTATION
#include <carbon.h>
#include <x_test.h>

int main(void) {
  x_test_register();
  return carbon_test_manager_run();
}

(…)

gcc -I test/include -I vendor/carbon -c test/src/carbon.c -o build/test/carbon.o

(…)

gcc --coverage build/x.o build/test/x_test.o build/test/test.o -Wl,--build-id -o build/test/carbon

Execution

(…)

./build/test/carbon

(…)

Additionally, it will create a file named carbon_results.xml with the execution results formatted as JUnit XML.

Code coverage

(…)