-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_ext.py
48 lines (39 loc) · 1.23 KB
/
test_ext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import glob
import sys
pytest_plugins = ['pytester']
def test_foo(testdir):
testdir.makepyfile(setup="""
from setuptools import setup, Extension
ext = Extension('foo', sources=['foo.c'])
setup(name='foo', py_modules=['test_foo'], ext_modules=[ext])
""",
test_foo="""
import foo
def test_bar():
assert foo.bar() == "Hello world"
""")
testdir.makefile('.c', foo=r"""
#include <Python.h>
static PyObject *bar(PyObject *self, PyObject *args)
{
return PyUnicode_FromString("Hello world");
}
static PyMethodDef methods[] = {
{"bar", bar, METH_VARARGS, "no docstring"},
{NULL}
};
static PyModuleDef moduledef = {
.m_base = PyModuleDef_HEAD_INIT,
.m_methods = methods,
.m_name = "foo",
.m_size = -1
};
PyMODINIT_FUNC PyInit_foo(void)
{
return PyModule_Create(&moduledef);
}
""")
testdir.run(sys.executable, 'setup.py', 'build')
build_dir, = glob.glob(str(testdir.tmpdir / 'build/lib.*'))
result = testdir.inline_run(build_dir)
result.assertoutcome(passed=1, failed=0)