Chapter 3
Chapter 3
package
D E V E L O P I N G P Y T H O N PA C K A G E S
James Fulton
Climate informatics researcher
The art and discipline of testing
Imagine you are working on this function
def get_ends(x):
"""Get the first and last element in a list"""
return x[0], x[-1]
(1, 0)
def test_get_ends():
assert get_ends([1,5,39,0]) == (1,0)
test_get_ends()
def test_get_ends():
assert get_ends([1,5,39,0]) == (1,0)
test_get_ends()
AssertionError:
...
def test_get_ends():
assert get_ends([1,5,39,0]) == (1,0)
assert get_ends(['n','e','r','d']) == ('n','d')
mysklearn/tests/ mysklearn/mysklearn/
|-- __init__.py |-- __init__.py
|-- preprocessing |-- preprocessing
| |-- __init__.py | |-- __init__.py
| |-- test_normalize.py | |-- normalize.py
| |-- test_standardize.py | |-- standardize.py
|-- regression |-- regression
| |-- __init__.py | |-- __init__.py
| |-- test_regression.py | |-- regression.py
|-- test_utils.py |-- utils.py
tests/preprocessing/test_normalize.py:10: AssertionError
================================== short test summary info ==================================
FAILED tests/preprocessing/test_normalize.py::test_mymax - assert -100 == 100 <-- test_mymax
================================ 1 failed, 5 passed in 0.17s ================================
James Fulton
Climate informatics researcher
Testing multiple versions of Python
This setup.py allows any version of Python from version 2.7 upwards.
setup(
...
python_requires='>=2.7',
)
setup(
...
python_requires='>=2.7',
)
Run tox
James Fulton
Climate informatics researcher
Introducing flake8
Standard Python style is described in PEP8
A style guide dictates how code should be laid out
flake8 features.py
4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4;
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4;
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
flake8 quadratic.py
4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4; # noqa
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
flake8 quadratic.py
4. ...
5. quadratic_1 = 6 * x**2 + 2 * x + 4; # noqa: E222
6. quadratic_2 = 12 * x**2 + 2 * x + 8
7. ...
flake8 quadratic.py
.
|-- example_package
| |-- __init__.py
| `-- example_package.py
|-- tests
| |-- __init__.py
| `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
`-- setup.py
[flake8] .
|-- example_package
ignore = E302 | |-- __init__.py
exclude = setup.py | `-- example_package.py
|-- tests
per-file-ignores = | |-- __init__.py
example_package/example_package.py: E222 | `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
.
|-- example_package
| |-- __init__.py
| `-- example_package.py
|-- tests
| |-- __init__.py
| `-- test_example_package.py
|-- README.rst
|-- LICENSE
|-- MANIFEST.in
|-- setup.py
`-- setup.cfg
1. # noqa : <code>
2. # noqa
3. setup.py → per-file-ignores
Most filtering