'running a package pytest with poetry

I am new to poetry and want to get it set-up with pytest. I have a package mylib in the following set-up

├── dist
│   ├── mylib-0.0.1-py3-none-any.whl
│   └── mylib-0.0.1.tar.gz
├── poetry.lock
├── mylib
│   ├── functions.py
│   ├── __init__.py
│   └── utils.py
├── pyproject.toml
├── README.md
└── tests
    └── test_functions.py

in test_functions I have

import mylib

However, when I run

poetry run pytest

it complains about mylib not being included. I can run

pip install dist/mylib-0.0.1-py3-none-any.whl

but that clutters my python environment with mylib. I want to use that environment as well for other packages.

My question is: What is the proper way to work with poetry and pytest?

My underlying python environment is a clean pyenv python 3.8. Using pyproject.toml I create a project based virtual environment for mylib.



Solution 1:[1]

You need to run poetry install to set up your dev environment. It will install all package and development requirements, and once that is done it will do a dev-install of your source code.

You only need to run it once, code changes will propagate directly and do not require running the install again.


If you have set up the virtual env that you want already, take care that it is activated when you run the install command. If you don't, poetry will try to create a new virtual env and use that, which is probably not what you want.

Solution 2:[2]

FYI you also need pytest specified as a dev dependency in pyproject.toml.

If you don't have that, poetry run will find the pytest instance in your home env, but that instance won't find the venv. I don't think the documentation makes that very clear.

Solution 3:[3]

There is a specific way to run pytest:

poetry run pytest

I couldn't run it just running pytest with the virtual environment activated. Nothing happens when I run.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 neves