Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest python src layout

Tags:

python

pytest

I'm trying out the one of the recommended python package layouts with the src directory.

enter image description here

My problem is when I run pytest from the command line my tests are not finding my python package. I tried running from the top level of this directory and within the tests directory but still getting ModuleNotFoundError exception. I'm running python 3.5 with pytest-3.5.0.

What is the recommend way to execute pytest from this type of python package layout?

like image 830
Danny Avatar asked Mar 31 '18 14:03

Danny


People also ask

What is src folder Python?

src/ — This is the folder that contains your python package codes. The structure of this folder varies on the application layout you have chosen. logs/ — log files are saved in this folder.

Where do I put Pytests?

While the pytest discovery mechanism can find tests anywhere, pytests must be placed into separate directories from the product code packages. These directories may either be under the project root or under the Python package.

What is __ init __ py?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.

What is Conftest py in pytest?

conftest.py is where you setup test configurations and store the testcases that are used by test functions. The configurations and the testcases are called fixture in pytest.


2 Answers

If you're using py.test to run the tests, it won't find the application as you must have not installed it. You can use python -m pytest to run your tests, as python will auto-add your current working directory to your PATH.

Hence, using python -m pytest .. would work if you're doing it in the src directory

The structure that I normally use is:

root
├── packagename
│   ├── __init__.py
│   └── ...
└── tests
    └── ...    

This way when I simply run python -m pytest in the root directory it works fine. Read more at: https://stackoverflow.com/a/34140498/1755083

Second option, if you still want to run this with py.test you can use:

PYTHONPATH=src/ py.test

and run it from the root. This simply adds the src folder to your PYTHONPATH and now your tests will know where to find the packagename package.

The third option is to use pip with -e to install your package in the editable mode. This creates a link to the packagename folder and installs it with pip - so any modification done in packagename is immediately reflected in pip's installation (as it is a link). The issue with this is that you need to redo the installation if you move the repo and need to keep track of which one is installed if you have more than 1 clones/packages.

like image 69
AbdealiJK Avatar answered Sep 23 '22 07:09

AbdealiJK


Just my two cents:

Don't use py.test, use pytest. The py.test is an old and deprecated command.

Notice Windows users; you may or may not have PYTHONPATH defined on your system. In such a case, I used the following commands to run tests:

set PYTHONPATH=src
pytest

To examine the PYTHONPATH:

echo %PYTHONPATH%
src

And the directory structure on containing src folder:

setup.py
src
    utils
        algo.py
        srel.py
        __init__.py
tests
    algo_test.py
    srel_test.py
    __init__.py

Finally, the documentation says that you might omit the __init__.py files. Removing them worked for me in this case.

like image 37
Jan Bodnar Avatar answered Sep 26 '22 07:09

Jan Bodnar