I like to use the following folder structure
NicksPythonPackage/
├─ src/
│ ├─ examplepythonpackage/
│ │ ├─ __init__.py
│ │ ├─ some_module.py
│ │ ├─ some_sub_package/
│ │ │ ├─ __init__.py
│ │ │ ├─ some_sub_module.py
├─ tests/
│ ├─ __init__.py
│ ├─ test_some_function.py
├─ pyproject.toml
├─ setup.cfg
├─ readme.md
├─ .gitignore
Note:
setup.cfg
will not be needed in the future. All config will go inpyproject.toml
.- I have included a dependancy on
pandas
in setup.cfg just for demonstration - I have included basic setup for
pytest
- Although
__init__.py
may sometimes be unnessisary in newer python versions, it is still required by setuptools to recognise modules AND submodules. __init__.py
files are blank- the
__init__.py
file in thetests/
folder is required forpytest-cov
to work
To run tests, the package will need to be installed using
pip install -e .
Where .
indicates that the current working directory is the package source.
Notes about testing:
- performing imports inside each the test function will prevent pytest from erroring out before providing feedback about what went wrong this is important if working in vscode; if you import at the top of the module, and the import failes, vscode provides poor error messaging in its UI
- by default, pytest will only search fot tests in:
- the directory specified in
pyproject.toml
- all files containing tests must start with
test_
....py
- all test functions names must begin with
def test_
...
- the directory specified in