Search code examples
pytesthy

How do I setup a hy project so that I can use pytest for testing


I'm trying to test a hy project with pytest but am having trouble with pytest discovering my tests. What needs to be done so that pytest can pick up tests written in hy? I've made the assumption that tests can be written in hy and discovered by pytest because of the native_tests section in the main hy repo. If this is a bad assumption, no need to read on. Please let me know.

My project root looks something like this:

src
  .hy program files
tests
  __init__.py
  test_*.hy test files where each test function is prefixed with test-*
pytest.ini

inside of pytest.ini I have the following:

[pytest]
python_functions = test_* is_test_* hyx_test_* hyx_is_test_*
testpaths = tests

I stole the python_functions section out of the hy repo's setup.cfg

Thank you!


Solution

  • The missing ingredient is Hy's confest.py, which defines hook functions pytest uses to discover tests. For example, we define pytest_collect_file like this:

    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