astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
13.6k stars 466 forks source link

How to test a virtual package? #1086

Closed dennisrall closed 4 months ago

dennisrall commented 4 months ago

How do I need to setup my project to be able to run tests for a virtual package? At the moment I am trying the standard way:

├── src
    ├── virt_package
        ├── __init__.py
        ├── main.py
├── tests
    ├── virt_package
        ├── test_main.py
└── pyproject.toml

How would I import a method foo from the main.py in the test_main.py? Is this even possible in virtual packages as the package itself is not installed?

dennisrall commented 4 months ago

I solved it now by adding the src directory to the PYTHONPATH with the following section in the pyproject.toml:

[tool.pytest.ini_options]
pythonpath = "src"

I will also make a PR to mention this in the docs.

jennydaman commented 2 months ago

https://github.com/astral-sh/rye/pull/1087 was not merged. Is there a current "best practice" for how to test virtual projects? (Or is the suggestion to simply not use the virtual project feature? I am not seeing any drawbacks about it.)

dennisrall commented 2 months ago

It was just a confusion about what virtual packages are

jennydaman commented 2 months ago

I guess I am confused too and the docs do need more elaboration on this topic.

https://rye.astral.sh/guide/docker/ <-- the guide here seems to suggest that projects which you intend to use Docker with are recommended to be virtual. It explains what the effects are but not why it is desirable.

dennisrall commented 2 months ago

As far as I know if your project is so big that you need tests it shouldn't be a virtual project. And therefore you have to adapt the Dockerfile. But I agree that this is still confusing

jennydaman commented 2 months ago

In case anyone else stumbles upon this thread, here's what I am doing:

For development on-the-metal, the rye project is not a virtual project, so that I can define my pytest tests in tests/.

My Dockerfile is what the docs recommend to use for virtual projects, but with an extra sed '/.../d' command to delete the line that installs the project as a package.

FROM python:3.12.3-alpine

WORKDIR /app
COPY requirements.lock ./
RUN sed -i' ' -e '/-e file:\./d' requirements.lock \
    && env PYTHONDONTWRITEBYTECODE=1 pip install --no-cache-dir -r requirements.lock

COPY src .
CMD ["fastapi", "run", "main.py"]