Closed dennisrall closed 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.
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.)
It was just a confusion about what virtual packages are
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.
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
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"]
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:
How would I import a method
foo
from themain.py
in thetest_main.py
? Is this even possible in virtual packages as the package itself is not installed?