Tests Demo: Package that wraps an API protected by an auth layer, and implements tests with pytest and mock. Also demonstrates how to separate offline and online tests, and safely manage secrets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Charles Reid 2d8f830a7a
update readme
2 years ago
docs/img update readme, add screenshot of test output 2 years ago
src update gh.py 2 years ago
tests add working integration/standalone modes. fix pyyaml requirement. lint. 2 years ago
.flake8 add flake8 config file 2 years ago
.gitignore add badges and image to readme 2 years ago
.pre-commit-config.yaml let pre-commit handle flake8 and black dependencies, use canned hooks 2 years ago
LICENSE add gitignore, license, and requirements file 2 years ago
Makefile clean up makefile 2 years ago
Readme.md update readme 2 years ago
common.mk add makefile files 2 years ago
environment.example add working integration/standalone modes. fix pyyaml requirement. lint. 2 years ago
requirements-test.txt add passing (real) tests, no standalone/integration split yet 2 years ago
requirements.txt add working integration/standalone modes. fix pyyaml requirement. lint. 2 years ago
setup.py add init pkg 2 years ago

Readme.md

alan-rickman

version-0.1.0

tests-unittesttests-mocktests-pytest

codestyle-blackcodestyle-flake8

pre-commit

python-3.9-3.10

Alan Rickman

A Python project that wraps an authenticated API, and implements tests with pytest and mock. Also demonstrates how to separate offline and online tests, and safely manage secrets.

What's in this repo?

  • environment - you must copy environment.example to environment and update the environment variable values before using this package. This environment file is ignored by git so that secrets are not accidentally added to the git repo.

    • Run source environment to load the variables.
  • src/ - contains a simple library wrapping a few calls to the GitHub API. This API requires a GitHub API token to call. This package uses the PyGithub package under the hood.

    • Run make requirements to install dependencies.
    • Run make build to build and install the package.
  • tests/ - contains tests implemented with pytest and unittest. Tests are categorized as offline (standalone) or online (integration). Which tests are run is controlled by environment variables.

    • Run make requirements-test to install packages required for tests.
    • Update environment to control whether standalone or integration tests are run.
    • Run make test to run the test suite.

Quick Start

git clone git@github.com:charlesreid1-toy-factory/alan-rickman.git
cd alan-rickman

Set up a virtual environment:

python3 -m virtualenv vp && source vp/bin/activate

Next, run make requirements to install dependencies.

Next, run make build to build and install the package.

Now you should be able to successfully import the package from Python:

$ python3

>>> import alan_rickman
>>>

Finally, to install the pre-commit hooks, run this command from the command line from the repository root:

pre-commit install

This will install pre-commit checks as specified in .pre-commit-config.yaml.

Running Tests

Before running tests, run make requirements-test to install packages required for tests.

Tests can run in standalone mode (offline) or integration mode (online). Integration mode requires a working GitHub API access token, which you can specify via the environment file or the ALAN_RICKMAN_GITHUB_ACCESS_TOKEN environment var.

The test mode is specified via an environment variable ALAN_RICKMAN_TEST_MODE.

Run make test to run the test suite.

Note: Running standalone tests does not require a real GitHub API access token. Integration tests do require a real access token. Any Github user's access token will work.

Screenshot of "make test" output

Environment Variables

  • ALAN_RICKMAN_HOME - location of the repository root, used by scripts
  • ALAN_RICKMAN_GITHUB_ACCESS_TOKEN - GitHub API access token
  • ALAN_RICKMAN_TEST_MODE - which mode to run tests in (standalone, integration, or both)

Technologies Used

Core library:

Tests:

Lint and style:

Patterns Demonstrated

  • Environment variables and environment file to safely manage secrets and access them from the program when needed
  • Makefile, and associated useful Makefile patterns
  • Pre-commit hooks to enforce linting and code style
  • Pytest to run the test suite
  • Separation of standalone and integation tests

(The use of the PyGithub library is not the feature of this repo, but PyGuthub is a really great library!)