如何设置Hy项目,以便可以使用pytest进行测试

时间:2020-07-03 15:21:20

标签: pytest hy

我正在尝试使用pytest测试一个hy项目,但是在pytest发现我的测试时遇到了麻烦。为了使pytest能够接受用hy编写的测试,需要做什么?我已经假设测试可以用hy编写并且可以通过pytest发现,因为主要hy回购中的native_tests部分。如果这是一个错误的假设,则无需继续阅读。请让我知道。

我的项目根目录如下:

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

我从回购协议的setup.cfg中偷走了python_functions部分

谢谢!

1 个答案:

答案 0 :(得分:0)

缺少的成分是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