0

Python と CFFI モジュールを使用して、C で単体テストを作成しようとしています。ほとんど機能していますが、サブディレクトリには使用できません。

テスト中、私のプロジェクトは次のようになります。

$ tree tests
tests/
├── sum.c
├── sum.h
├── tests_units.py
...

$ python3 tests_unit.py

...

OK

しかし、プロジェクト用に変換するとき:

$ tree
.
├── Makefile
├── src
│   ├── sum.c
│   └── sum.h
│   └── ...
└── tests
    └── tests_units.py

make checkの実行は次のとおりです。

check:
    python3 tests/tests_units.py

そして、私は自分のテストファイルを適応させるためにそうしました:

import unittest
import cffi
import importlib

def load(filename):
    # load source code
    source = open(filename + '.c').read()
    includes = open(filename + '.h').read()

    # pass source code to CFFI
    ffibuilder = cffi.FFI()
    ffibuilder.cdef(includes)
    ffibuilder.set_source(filename + '_', source)
    ffibuilder.compile()

    # import and return resulting module
    module = importlib.import_module(filename + '_')

    return module.lib


class SumTest(unittest.TestCase):
    def setUp(self):
        self.module = load('src/sum')

    def test_zero(self):
        self.assertEqual(self.module.sum(0), 0)

if __name__ == '__main__':
    unittest.main()

この行に注意してください:

self.module = load('src/sum')

だから私のログは

...
Traceback (most recent call last):
File "tests/tests_units.py", line 28, in setUp
  self.module = load('src/sum')
File "tests/tests_units.py", line 17, in load
  ffibuilder.set_source(filename + '_', source)
File "/usr/local/lib/python3.6/site-packages/cffi/api.py", line 625, in set_source
raise ValueError("'module_name' must not contain '/': use a dotted "
ValueError: 'module_name' must not contain '/': use a dotted name to make a 'package.module' location
...

しかし、これはモジュールではなく、単純なディレクトリです。

解決策はありますか?

よろしく。

4

1 に答える 1