Skip to main content

Automated Testing Toy Example

In this project, we'll practice using Pytest to automatically test the functionality of three kinds of files:

  • Python Modules: Can we import the functions? Do they behave as expected?
  • Python Scripts: Does the script run without error? Does it behave as expected?
  • Jupyter Notebooks: Does the notebook run without error? Does it behave as expected?

Toy Dataset: Titanic Survival Analysis

Did passenger class, sex, and age have an effect of who survived the Titanic disaster of 1912? This analysis says yes!

Directions

Write Automated tests for each python code file

  1. tests/test_utils.py
  • Test that the titanic_utils.is_adult() function behaves correctly on three different ages.

  • For example, assert is_adult(11) == False

  1. tests/test_data_download_script.py
  • Test that the scripts/download_data.py file runs without errors (for extra credit, that it creates the data/raw/titanic.csv file)

  • The runpy.run_path() function is particularly helpful for this.

  • os.path.exists(), os.remove(), and shutils.rmtree() can also be helpful here.

  1. tests/test_analysis_notebook.py
  • Test that the notebooks/anlayze_data.ipynb file runs without errors (for extra credit, that it creates the results/survival_rate.jpg file)

  • subprocess.check_output() is a useful function here, when used to call jupyter nbconvert --to notebook --execute my_notebook.ipynb

  • os.path.exists(), os.remove(), and shutils.rmtree() can also be helpful here.

Push your changes to a new fork of this repository

Add a GitHub Action that runs the tests on push

Do this in the web browser. Use the Python Package using Anaconda or Python Application templates under the GitHub Actions tab to get started.

Tip: Start with a small number of tests, re-running the pipeline stepwise to ensure that it continues to work.

Packaging and Publishing

Publishing on PyPI

Step Description
pyproject.toml Make the file in the root directory that contains the build instructions and project metadata
pip install build; python -m build Builds distribution files in a venv using the pyproject.toml file
pip install twine; twine check dist/* Check that the dist files have the needed metadata
twine upload --repository-url=https://test.pypi.org/legacy/ dist/* Upload the dist files. Make sure you have an account on the test.pypi.org site.

Github Actions: Deploy on Tagged Push or Release

Step Description
git tag -a v1.4 -m "my version 1.4"; Make an annotated tag
git push origin --tags Push tags
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') In the GA step, only does the step if there was a tag
release: types: [ created ] Run workflow on GH release creation

Metadata

Release files for nickscoolproject2342 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for nickscoolproject2342 0.0.1
File Interpreter ABI Platform
nickscoolproject2342-0.0.1-py3-none-any.whl Python 3 none any Details

Release files / nickscoolproject2342-0.0.1-py3-none-any.whl

Download URL nickscoolproject2342-0.0.1-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2fbfe49ef989046920f56aad87670e1a460a1a6cf006642277324976e0370bc0
BLAKE2b-256 checksum
How to use checksums
8e06ee281ff4d8823eafb1bcd4715b37132337d51814b0ee3f1c6fa8cdffd8bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page