如何设置 hy 项目以便我可以使用 pytest 进行测试
How do I setup a hy project so that I can use pytest for testing
我正在尝试使用 pytest 测试一个 hy 项目,但在 pytest 发现我的测试时遇到了问题。需要做什么才能让 pytest 可以选择用 hy 编写的测试?由于主 hy 回购中的 native_tests
部分,我假设测试可以用 hy 编写并由 pytest 发现。如果这是一个错误的假设,则无需继续阅读。请告诉我。
我的项目根目录如下所示:
src
.hy program files
tests
__init__.py
test_*.hy test files where each test function is prefixed with test-*
pytest.ini
在 pytest.ini 里面我有以下内容:
[pytest]
python_functions = test_* is_test_* hyx_test_* hyx_is_test_*
testpaths = tests
我从 hy repo 的 setup.cfg
中偷走了 python_functions 部分
谢谢!
缺少的成分是 Hy 的 confest.py
,它定义了 pytest 用来发现测试的钩子函数。比如我们这样定义pytest_collect_file
:
def pytest_collect_file(parent, path):
if (path.ext == ".hy"
and NATIVE_TESTS in path.dirname + os.sep
and path.basename != "__init__.hy"):
if hasattr(pytest.Module, "from_parent"):
pytest_mod = pytest.Module.from_parent(parent, fspath=path)
else:
pytest_mod = pytest.Module(path, parent)
return pytest_mod
我正在尝试使用 pytest 测试一个 hy 项目,但在 pytest 发现我的测试时遇到了问题。需要做什么才能让 pytest 可以选择用 hy 编写的测试?由于主 hy 回购中的 native_tests
部分,我假设测试可以用 hy 编写并由 pytest 发现。如果这是一个错误的假设,则无需继续阅读。请告诉我。
我的项目根目录如下所示:
src
.hy program files
tests
__init__.py
test_*.hy test files where each test function is prefixed with test-*
pytest.ini
在 pytest.ini 里面我有以下内容:
[pytest]
python_functions = test_* is_test_* hyx_test_* hyx_is_test_*
testpaths = tests
我从 hy repo 的 setup.cfg
中偷走了 python_functions 部分谢谢!
缺少的成分是 Hy 的 confest.py
,它定义了 pytest 用来发现测试的钩子函数。比如我们这样定义pytest_collect_file
:
def pytest_collect_file(parent, path):
if (path.ext == ".hy"
and NATIVE_TESTS in path.dirname + os.sep
and path.basename != "__init__.hy"):
if hasattr(pytest.Module, "from_parent"):
pytest_mod = pytest.Module.from_parent(parent, fspath=path)
else:
pytest_mod = pytest.Module(path, parent)
return pytest_mod