pytesthy

No diffs from pytest AssertionErrors when straying from common test file name scheme


I have the following conftest.py for a pytest plugin for hy, adapted from the official hy conftest, but AssertionErros are not diffed for files beginning with test-, only test_, even when using -vv on the command line.

import os
from pathlib import Path

import pytest

import hy

NATIVE_TESTS = Path.cwd().joinpath("tests", "native_tests")

# https://github.com/hylang/hy/issues/2029
os.environ.pop("HYSTARTUP", None)


def pytest_collect_file(parent, path):
    if (
        path.ext in (".hy", ".py")
        and (
            path.basename.startswith("test_")
            or path.basename.startswith("test-")
            or path.basename.endswith("_test")
            or path.basename.endswith("-test")
        )
        # Mimics https://github.com/hylang/hy/blob/master/conftest.py#L38:
        and (
            (not NATIVE_TESTS.exists())
            or (
                NATIVE_TESTS.exists() and (str(NATIVE_TESTS) in (path.dirname + os.sep))
            )
        )
        and path.basename != "__init__.hy"
    ):
        path = Path(path)
        path.touch(exist_ok=True)
        return pytest.Module.from_parent(parent, path=path)

I have the following __init__.py as well, but it doesn't seem to be working:

import pytest

pytest.register_assert_rewrite("pytest_hy")

Solution

  • Mangling applies to names in Hy code, not file names. So if you have a file named foo_bar.hy, you can import it as (import foo-bar), but calling your file foo-bar.hy is not really supported in any fashion by Hy's import system, let alone by pytest. In particular, (import foo-bar) will look for foo_bar.hy, as just mentioned, not foo-bar.hy.