Module 3: Installable package and pytest
In the last module, you introduced modules, classes and a new notebook in you repo. In this module, you will add tests to your code base. But first, you will make your package installable.
- Create new branch “package-test” (Make sure changes from last module have been merged, and that you start from the main branch)
- 3.1 Installable package
- Organize the files into folders and add
setup.py
. Call your packagetscleaner
.- subfolders: tscleaner, scripts, notebooks, tests
- make an init-file
__init__.py
in the tscleaner folder with the following content:
from .cleaning import SpikeCleaner, FlatPeriodCleaner, OutOfRangeCleaner from .plotting import plot_timeseries
- create a
setup.py
in the root with the following content (change with your data):
from setuptools import setup, find_packages setup(='MyPackageName', name='0.0.1', version='https://github.com/mypackage.git', url='Author Name', author='author@gmail.com', author_email='Description of my package', description=find_packages(), packages=['numpy', 'matplotlib'], install_requires )
- Organize the files into folders and add
- Install the package in editable mode, by running the below command from the project root. -
> pip install -e .
- Modify import statements in
notebook_A
and scriptmain.py
and make sure they run. - Modify the cleaner tools by raising exceptions for invalid inputs.
- Move the csv file to
/tests/testdata
and update notebook with relative path to the file
- Modify import statements in
- 3.2 Pytest
- Make sure
pytest
andpytest-cov
are installed - Write unit tests with pytest in the
/tests
folder. Create an empty__init__.py
file in the folder. Create a filetest_cleaning.py
and create at least three tests that verify that the cleaning tools work as intended - If all your tests are failing, consider if you have given the right requirements in the
setup.py
… - [Optional] Consider to make a fixture that reads the csv file and you can read in all tests
- Run the tests from the commandline by writting
>pytest
in the project root (can you also run the tests from VSCode?) - Assess the test coverage with
>pytest --cov=tscleaner tests
- [Optional] Get coverage as html with
>pytest --cov=tscleaner --cov-report html
(check the index.html in the htmlcov subfolder afterwards)
- Make sure
- Create pull request in GitHub and “request review” from your reviewers
- Get feedback, Adjust code until approval, then merge (and delete branch)