Skip to content

Instantly share code, notes, and snippets.

@svelleweetakealot
Last active November 3, 2017 11:08
Show Gist options
  • Save svelleweetakealot/1e5ea2976f29ee5b701991ad840c2f8f to your computer and use it in GitHub Desktop.
Save svelleweetakealot/1e5ea2976f29ee5b701991ad840c2f8f to your computer and use it in GitHub Desktop.
C testing using CFFI, pytest
#include "add.h"
int add(int a, int b) {
return a + b;
}
extern int add(int, int);
import cffi
import importlib
import pytest
import uuid
# taken from https://www.youtube.com/watch?v=zW_HyDTPjO0
def load(filename):
name = filename + "_" + uuid.uuid4().hex
source = open(filename + ".c").read()
includes = open(filename + ".h").read()
ffibuilder = cffi.FFI()
ffibuilder.cdef(includes)
ffibuilder.set_source(name, source)
ffibuilder.compile()
module = importlib.import_module(name)
return module.lib
addmod = load('add')
def test_add():
assert addmod.add(12, 13) == 25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment