Running Python unit tests without creating in-place extensions

I have a Python 3.6 package that includes some Cython extensions. Suppose its contents are as follows:

# example/example.pyx

def add(int a, int b):
    return a + b

I can build this setuptoolsusing this script

# setup.py

from setuptools import setup, Extension
from Cython.Build import cythonize

example_ext = Extension(
    name='example.example',
    sources=['example/example.pyx'],
)

setup(
    name='Example package',
    packages=['example'],
    ext_modules=cythonize([example_ext]),
)

Now I can add unit test:

# example/tests/test_example.py

import unittest
from example.example import add

class TestExample(unittest.TestCase):
    def test_add(self):
        result = add(4, 5)
        self.assertEqual(result, 9)

However, if I run python -m unittest discover, the tests fail with help ImportError, since it cannot find the compiled Cython module. This happens even if I already built the module using python setup.py build_ext.

The only way I'm working on testing is to create a Cython module in place with python setup.py build_ext -i, but it doesn't seem to be necessary. Is there a way to tell the library unittesthow to find compiled Cython modules if I don't create in place?

+4

Source: https://habr.com/ru/post/1680149/


All Articles