Differences in how modules are imported between running pytest
on a directory or a file
#11588
-
We use pytest for Hy, both for the compiler's own tests and for tests of other Hy programs. Mostly, this works well, but one flaw I've noticed is that calling Here's a minimal example: mkdir -p tests
echo >tests/__init__.py '
import hy'
echo >tests/test_foo.hy '
(defn test_bar []
(assert (= (+ 1 1) 2)))'
echo >conftest.py '
import hy, pytest
def pytest_collect_file(file_path, parent):
if file_path.suffix == ".hy":
return pytest.Module.from_parent(parent, path=file_path)' You should see that
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The reason is this: pytest uses "assertion rewriting" as a way to implement its path -> module spec -> hy -> module object -> [path -> ast -> ast rewrite -> module object] The "[path -> ast]" ( OK so pytest's assertion rewriting feature seemingly can't work with hy, so how come "modules not passed explicitly on the command line are only rewritten if they match the naming convention for test files". The default value of the I can't think of any good solutions for you other than turning off assertion rewriting ( |
Beta Was this translation helpful? Give feedback.
The reason is this: pytest uses "assertion rewriting" as a way to implement its
assert
magic. It rewrites test modules at the AST level and loads the rewritten AST instead of the original. The python import system doesn't have an "AST transform pipeline" concept, only "find" (path -> module spec) and "load" (module spec -> module object). So the flow is this (pytest part in [...]):path -> module spec -> hy -> module object -> [path -> ast -> ast rewrite -> module object]
The "[path -> ast]" (
ast.parse()
) part can't work because the file is hy syntax not python syntax. There is no way to go "module object -> ast" without reparsing the file.OK so pytest's assertion rewriting feature seemi…