Giter VIP home page Giter VIP logo

chili's Introduction

Chili - testrunner for C in C

What is Chili

A test runner for C programmers that makes it easy and fun to write nice and slim unit tests with minimum hazzle. You write your Chili tests completely without build dependencies.

Chili is written in C with minimal dependencies on external libraries. The idea is that Chili could be used on target for embedded development.

Installation

~$ make && make install

Chili is Linux only.

Simplest possible unit test

Create new file: unittests.c

#include <stdio.h>

int test_that_succeeds()
{
    printf("Returning 1 indicates success\n");
    return 1;
}

Tests should always be named with the prefix test_. Also note that the funcion must NOT be static.

Build the unit test:

~$ gcc unittests.c -fPIC --shared -o unittests.so

This builds a shared library with the test as an exported symbol. This what Chili finds and executes.

Run the unit test:

~$ chili all ./unittests.so
./unittests.so: test_that_succeeds: Success [0]
Executed: 1, Succeeded: 1, Failed: 0, Errors: 0

The all command executes all tests in the shared library, in this case only one test. Note that the output of printf is not shown in the output above, Chili will default to suppress output of all succeeding tests.

Unit test return values

Unit tests should always return an int and should not take any parameters. Chili interprets the returned values the following way:

  • Positive values are interpreted as a successful/green test.
  • Zero are interpreted as a failing/red test.
  • Negative values are interpreted as an unexpected error occured while executing the test. Chili will not execute any more tests in this suite when it encounters a test that returns a negative value.

A test could also crash during it's execution, this is reported as a crash but Chili will continue executing other tests.

#include <stdio.h>

int test_that_succeeds()
{
    printf("Positive indicates success\n");
    return 1;
}

int test_that_fails()
{
    printf("Zero indicates failure\n");
    return 0;
}

int test_that_crashes()
{
    int *x = (int*)0;
    printf("This will crash\n");
    *x = 0;
}

int test_with_error()
{
    printf("This is an error\n");
    return -1;
}

Build this into a shared library named unittests.so and let Chili execute the tests:

~$ chili all ./unittests.so
./unittests.so: test_that_succeeds: Success [0]
./unittests.so: test_that_fails: Failed [1]
>>>Capture start
Zero indicates failure
<<< Capture end
./unittests.so: test_that_crashes: Crashed [2]
>>> Capture start
This will crash
<<< Capture end
./unittests.so: test_with_error: Error [3]
>>> Capture start
This is an error
<<< Capture end
Executed: 4, Succeeded: 1, Failed: 1, Errors: 2

As shown above prints are only shown on tests with any type of problem.

Suite setup functions

Debugging a unit test

To debug a problematic unit test do like this. To debug the test test_that_crashes in the following module.

#include <stdio.h>

int test_that_crashes()
{
    int *x = (int*)0;
    printf("This will crash\n");
    *x = 0;
}

To be able to debug, the tests and other linked modules need to be built with debug information:

~$ gcc unittests.c -fPIC --shared -g -o unittests.so

Debugging is done with Chilis debug command and the name of the test:

~$ chili debug ./unittests.so:test_that_crashes
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc
...
Attaching to process 1637
Reading symbols from ...
Reading symbols ./unittests.so..done
...
Breakpoint 1 at 0x7fa51cab1766: file unittest.c, line 3.
Continuing with signal SIGCONT.

Breakpoint 1, test_that_crashes () at unittest.c:3
3       int *x = (int*)0;
(gdb)

Gdb of course needs to be installed for this to work.

Other usable commands

To list all tests in a suite use the list command:

~$ chili list ./unittests.so
./unittests.so:test_one
./unittests.so:test_two

Format of listed test are on the same format that is used to run tests by name. The named command runs all named tests that is found in a file or from stdin:

~$ chili list ./unittests.so | grep two | chili named
./unittests.so: test_two: Success [0]
Executed: 1, Succeeded: 1, Failed: 0, Errors: 0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.