uv icon indicating copy to clipboard operation
uv copied to clipboard

uv run pytest not recognizing the project module

Open EdmundsEcho opened this issue 1 year ago • 9 comments

My project structure is:

project_name/module_name/parser.py
project_name/module_name/__init__.py
project_name/tests/test_parser.py

The test_parser.py references the module:

from module_name.parser import print_summaries

I can build and run the parser but I cannot seem to get uv run pytest to run without getting a module not found error.

Apologies for the hyper newby question here.

EdmundsEcho avatar Nov 20 '24 19:11 EdmundsEcho

You'll probably need to define a build system so your project is installed in the environment and discoverable by pytest.

zanieb avatar Nov 20 '24 20:11 zanieb

Thank you for the response. The link was helpful.

In the meantime, the following gets pytest to run from the project root:

set -x PYTHONPATH .; uv run pytest

Interestingly, ruff seems to resolve the dependency without the env setting.

EdmundsEcho avatar Nov 20 '24 20:11 EdmundsEcho

It's possible that https://docs.pytest.org/en/stable/explanation/goodpractices.html could help here, they have a section for your case: Tests as part of application code

bluss avatar Nov 20 '24 20:11 bluss

I did some more reading here, and your case should work with:

❯ uv run -m pytest

or

❯ touch tests/__init__.py
❯ uv run pytest --pyargs my_module

as described in the pytest documentation.

zanieb avatar Nov 20 '24 21:11 zanieb

@zanieb Thank you!!

What you provided is definitely worth including in the documentation that describes setting up an application with uv.

Even better, it may be worthwhile to include a init command that includes the file structure for testing hello.py ("It works!"). This would require having an opinion on the file structure... which is good. I believe the structure that I set-up is a solid, straightforward norm, but I actually don't care :-)) What I mean by that is that whatever uv chooses to go with, I would go with... I want to build and setup quickly, including tests (just like rust, which includes a unit test in the initialized app).

EdmundsEcho avatar Nov 21 '24 15:11 EdmundsEcho

The problem is that, in Rust, there's a standardized test runner. In Python, there is not. We'd get complaints if we configured the project with pytest by default, though maybe we should anyway.

zanieb avatar Nov 21 '24 18:11 zanieb

@zanieb My take on this is that anyone with a POV has what they need (expertise) to make the change to how they like it. The goal of uv is to get a person up and running.

EdmundsEcho avatar Nov 21 '24 18:11 EdmundsEcho

had same issue where pytest couldn't find the package after converting from poetry to uv

fixed by adding this to pyproject.toml:

[build-system]
requires = ['setuptools']
build-backend = 'setuptools.build_meta'

medecau avatar Nov 21 '24 21:11 medecau

This is a common issue with PyTest itself: https://stackoverflow.com/questions/10253826/path-issue-with-pytest-importerror-no-module-named. I will add two other options.

You can either solve this by adding the following to your pyproject.toml file:

[tool.pytest.ini_options]
addopts = [
    "--import-mode=importlib",
]
pythonpath = [
  "."
]

Or adding the tests directly to your project_name/module_name. This is the method used in the FastAPI documentation: https://fastapi.tiangolo.com/tutorial/testing/#testing-file

p13rr0m avatar Dec 11 '24 20:12 p13rr0m

In a mono repo (uv workspace), adding the above to the root pyproject.toml is definitely the solution. Thanks for the documentation links @p13rr0m.

chamalgomes avatar Apr 14 '25 05:04 chamalgomes