PyTest and Best Practices - How to test with a pip installed package
How to use your installed package to test against
This is a description of what I had read somewhere, but was having a hard time finding it. I just found one of the source that I was thinking about, though what I list below seems to follow in the same vein, but with a little more wordage.
The thing I saw initially, but couldn't find for a bit
Not sure how I missed it, but ... In the article Good Integration Practices - Install package with pip, it says it pretty clearly.
For development, we recommend you use venv for virtual environments and pip for installing your application and any dependencies, as well as the pytest package itself. This ensures your code and dependencies are isolated from your system Python installation.
I'm stealing the text here, but it should be quoted like the text above, but it's a pain to do that all the time. Maybe I'll fix it later. Anyway ...
Next, place a setup.py file in the root of your package with the following minimum content:
from setuptools import setup, find_packages
setup(name="PACKAGENAME", packages=find_packages())
Where PACKAGENAME is the name of your package. You can then install your package in “editable” mode by running from the same directory:
pip install -e .
which lets you change your source code (both tests and application) and rerun tests at will. This is similar to running python setup.py develop or conda develop in that it installs your package using a symlink to your development code.
I have quit the just copying and pasting here.
There is other good information here that I should make sure that I read and understand.
Another way of looking at it
Here is the answer that I think is best (after figuring out how to package my project so I can use it -- note - I have not actually gotten to use it yet, so ...) -- pytest cannot import module while python can.
User erickfis had this to say:
My 2 cents on this: pytest will fail at chance if you are not using virtual environments. Sometimes it will just work, sometimes not.
Therefore, the solution is:
- remove pytest with pip uninstall
- create your venv
- activate your venv
- pip install your project path in editable mode, so it will be treated by pytest as a module (otherwise, pytest wont find your internal imports). You will need a setup.py file for that
- install your packages, including pytest
finally, run your tests
The code, using windows PowerShell:
powershell pip uninstall pytest python.exe -m venv my_env .\my_env\Scripts\activate (my_env) pip install -e . (my_env) pip install pytest pytest-html pandas numpyThen finally
powershell (my_env) pytest --html="my_testing_report.html"An example of setup.py, for
pip install -e:```python import setuptools
setuptools.setup( name='my_package', version='devel', author='erickfis', author_email='erickfis@gmail.com', description='My package', long_description='My gooood package', packages=setuptools.find_packages(), classifiers=[ 'Programming Language :: Python :: 3', 'Operating System :: OS Independent', ], include_package_data=True ) ```
erickfis - on [2020-04-22 20:17]
So, I think that the several of the steps above (minus the uninstall of pytest) can be achieved with pipenv shell which should be the same as the python -m venv my_env.
Then, the same step(s) could be followed:
pip install -eto get the project in editable modepip install pytest pytest-html pandas numpy- I think these are what he needed, which is fair. Will have to play with this to get it working the way it should
Will want to put all of this in my Makefile as I go, so I don't have to remember what I did, but have a repeatable way to replicate it!