I will suggest you the setuptools
approach (which makes your package distributable :D)
Project files' structure:
.
├── sample
│ ├── __init__.py
│ └── return_self.py
├── setup.cfg
├── setup.py
└── tests
└── test_return_self.py
where the sample/
directory matches the package's name and also must contain the source.
Minimal setup.py file content:
from setuptools import setup
setup(
setup_requires=['pytest-runner'],
tests_require=['pytest'],
name='sample'
)
Here we are configuring our test environment (you can extend the tests_require
variable to include more testing requirements).
setup.cfg file content:
[aliases]
test=pytest
And here we specify we want to run the command pytest every time the developer does: python setup.py test
tests/test_return_self.py
from pytest import *
from sample.return_self import return_self
def test_return_self():
assert return_self(4) == 4
sample/return_self.py
def return_self(n):
return n
So, the next thing to do is to run:
python setup.py develop
to make your package available (while running the tests). If you're having problems with permission denied issues: append the --user
option to the previous command to instruct python you wanna use the package without root permissions --in short the package will be installed into userland directories.
And finally run the tests using:
python setup.py test
Notes:
- By using this approach, you'll be able to alter your project code on the fly (
python setup.py develop
needs to be run just once)
- No ugly statements to inject the source directory into the current python path.
References:
Integrating with setuptools / python setup.py test / pytest-runner
Building and Distributing Packages with Setuptools