Skip to content

Commit

Permalink
Make test_doctest_ufunc work with pytest 4.6
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed Feb 7, 2022
1 parent 3345ea7 commit ced3099
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 127 deletions.
2 changes: 0 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import pytest

collect_ignore = ['ufunc_example']


def _wrap_docstring_in_func(func_name, docstring):
template = textwrap.dedent(r"""
Expand Down
42 changes: 0 additions & 42 deletions tests/test_doctest_ufunc.py

This file was deleted.

110 changes: 110 additions & 0 deletions tests/test_doctestplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,113 @@ def test_remote_data_ignore_warnings(testdir):
)
testdir.inline_run(p, '--doctest-plus', '--doctest-rst', '--remote-data').assertoutcome(passed=1)
testdir.inline_run(p, '--doctest-plus', '--doctest-rst').assertoutcome(skipped=1)


@pytest.importorskip('numpy')
def test_example(testdir):
# Create and build example module
testdir.makepyfile(module1="""
def foo():
'''A doctest...
>>> foo()
1
'''
return 1
""")
testdir.makepyfile(_module2="""
from _module2 import foo
""")
testdir.makepyfile(setup="""
from setuptools import setup, Extension
import numpy as np
ext = Extension('_module2', ['_module2.c'],
extra_compile_args=['-std=c99'],
include_dirs=[np.get_include()])
setup(name='example', py_modules=['module1', 'module2'], ext_modules=[ext])
""")
testdir.makefile('.c', _module2="""
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
#include <numpy/ufuncobject.h>
#include <Python.h>
static double foo_inner(double a, double b)
{
return a + b;
}
static void foo_loop(
char **args,
const npy_intp *dimensions,
const npy_intp *steps,
void *NPY_UNUSED(data)
) {
const npy_intp n = dimensions[0];
for (npy_intp i = 0; i < n; i ++)
{
*(double *) &args[2][i * steps[2]] = foo_inner(
*(double *) &args[0][i * steps[0]],
*(double *) &args[1][i * steps[1]]);
}
}
static PyUFuncGenericFunction foo_loops[] = {foo_loop};
static char foo_types[] = {NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE};
static void *foo_data[] = {NULL};
static const char foo_name[] = "foo";
static const char foo_docstring[] = ">>> foo(1, 2)\n3.0";
static PyModuleDef moduledef = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "_module2",
.m_size = -1
};
PyMODINIT_FUNC PyInit__module2(void)
{
import_array();
import_ufunc();
PyObject *module = PyModule_Create(&moduledef);
if (!module)
return NULL;
PyObject *obj = PyUFunc_FromFuncAndData(
foo_loops, foo_data, foo_types, 1, 2, 1, PyUFunc_None, foo_name,
foo_docstring, 0);
if (!obj)
{
Py_DECREF(module);
return NULL;
}
if (PyModule_AddObject(module, foo_name, obj) < 0)
{
Py_DECREF(obj);
Py_DECREF(module);
return NULL;
}
return module;
}
""")
testdir.run(sys.executable, 'setup.py', 'build')
build_dir, = glob.glob(str(testdir.tmpdir / 'build/lib.*'))

# Run pytest without doctests: 0 tests run
result = testdir.runpytest(build_dir)
result.assert_outcomes(passed=0, failed=0)

# Run pytest with doctests: 1 test run
result = testdir.runpytest(build_dir, '--doctest-modules')
result.assert_outcomes(passed=1, failed=0)

# Run pytest with doctests including ufuncs: 2 tests run
result = testdir.runpytest(build_dir, '--doctest-plus', '--doctest-modules', '--doctest-ufunc')
result.assert_outcomes(passed=2, failed=0)
68 changes: 0 additions & 68 deletions tests/ufunc_example/_module2.c

This file was deleted.

7 changes: 0 additions & 7 deletions tests/ufunc_example/module1.py

This file was deleted.

1 change: 0 additions & 1 deletion tests/ufunc_example/module2.py

This file was deleted.

7 changes: 0 additions & 7 deletions tests/ufunc_example/setup.py

This file was deleted.

0 comments on commit ced3099

Please sign in to comment.