【问题标题】:How do I setup a hy project so that I can use pytest for testing如何设置一个 hy 项目以便我可以使用 pytest 进行测试
【发布时间】:2020-07-03 15:21:20
【问题描述】:

我正在尝试使用 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

我从 hy repo 的 setup.cfg 中偷走了 python_functions 部分

谢谢!

【问题讨论】:

    标签: pytest hy


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-11
      • 2013-04-14
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多