jeudi 27 août 2015

py.test to test Cython C API modules

I'm trying to set up unit tests for a Cython module to test some functions that do not have python interface. The first idea was to check if .pyx files could directly be used by py.test's test runner, but apparently it only scans for .py files.

Second idea was to write the test_* methods in one Cython module, which could then be imported into a plain .py file. Let say we have a foo.pyx module with the contents we want to test:

cdef int is_true():
    return False

then a test_foo.pyx module that uses the C API to test the foo module:

cimport foo

def test_foo():
    assert foo.is_true()

and then import these in a plain cython_test.py module that would just contain this line:

from foo_test import *

The py.test test runner does find test_foo this way, but then reports:

/usr/lib/python2.7/inspect.py:752: in getargs
    raise TypeError('{!r} is not a code object'.format(co))
E   TypeError: <built-in function test_foo> is not a code object

Is there any better way to test Cython C-API code using py.test?

Aucun commentaire:

Enregistrer un commentaire