mrh1997 / headlock

An adapter for making C code testable from Python (see https://headlock.readthedocs.io/en/latest)
MIT License
3 stars 0 forks source link
bridge bridging c embedded-c iot python tdd test-driven-development testing unittest

Headlock - Python/C Bridge for Unittesting

buildstate docstate

About

This is an adapter for testing C code via tests written in python. When being combined i.e. with pytest it provides a very powerful and convinient way of writing (unit-/integration-) tests for C code.

In contrary to other C/Python bridges (like ctypes, cffi, swing, ...) the goals of this projects are:

Explicitly Non-Goals Are:

Sample

This piece of C-code contains a macros, a struct a function implementation and a function that is relying on (which should be mocked):

#include "underlying_module.h"

struct ops_t
{
    int a, b;
} ;

#define MACRO_2   (MACRO_1 + 1)

int func(struct ops_t * p)
{
    return underlying_func(p->a + p->b + MACRO_1);
}

You can access it from python through headlock like:

from headlock.testsetup import TestSetup, CModule

@CModule('dummy.c', MACRO_1=1)
class TSSample(TestSetup):
    def underlying_func_mock(self, param):
        return param.val + 4000

with TSSample() as ts:
    ops = ts.struct.ops_t(a=ts.MACRO_2, b=20)
    assert ts.func(ops.ptr).val == 4021

This demonstrates how:

Status

Currently this is alpha.

For a list of planned but not yet implemented features please refer to Development Status