dabeaz / sly

Sly Lex Yacc
Other
816 stars 107 forks source link

Pytest can not be run top-level #93

Closed dvzrv closed 2 years ago

dvzrv commented 2 years ago

During packaging this project I attempted to run pytest in the root of the project.

This by default unfortunately leads to failing imports for sly:

============================= test session starts ==============================
platform linux -- Python 3.10.2, pytest-6.2.5, py-1.11.0, pluggy-0.13.1 -- /usr/bin/python
cachedir: .pytest_cache
rootdir: /build/python-sly/src/sly-0.4
collecting ... collected 0 items / 2 errors

==================================== ERRORS ====================================
______________________ ERROR collecting tests/test_lex.py ______________________
ImportError while importing test module '/build/python-sly/src/sly-0.4/tests/test_lex.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_lex.py:2: in <module>
    from sly import Lexer
E   ModuleNotFoundError: No module named 'sly'
____________________ ERROR collecting tests/test_parser.py _____________________
ImportError while importing test module '/build/python-sly/src/sly-0.4/tests/test_parser.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_parser.py:2: in <module>
    from sly import Lexer, Parser
E   ModuleNotFoundError: No module named 'sly'
=========================== short test summary info ============================
ERROR tests/test_lex.py
ERROR tests/test_parser.py
!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!
============================== 2 errors in 0.06s ===============================

Pytest is fairly clever in figuring out where the test suite is, but in this case is not able to derive the location of the sly module on its own. By providing an empty __init__.py file in the tests directory this can be fixed though! :)

dabeaz commented 2 years ago

Works on my machine. Using python3 -m pytest in the top directory.