Skip to main content

A Pytest plugin to access the system's keyring to provide credentials for tests

Project description

pytest-keyring

Pytest plugin to access any configured keyring using the keyring package.

Motivation

Frequently, tests require access to services, like databases, REST APIs, or blob storages. These services are sometimes hard or impossibel to mock or reproduce in a test environment. In particular, functional or end-to-end tests will be inclined to interact with real instances of these services. Accessing these services can require credentials, and using the keyring can be an alternative to populating CI environment variables with credentials.

Installation

Install with:

python -m pip install pytest-keyring

Python versions 3.8 to 3.12 are supported.

Usage

Accessing credentials in keyring

Any test arguments whose names match the prefixes configured by --keyring-password-prefix and --keyring-credential-prefix ("password" and "credential" by default) will be replaced by the corresponding password or credential, respectively:

def test_with_database(credential_postgres_dbuser):
    client = connect(
        username=credential_postgres_dbuser.username,
        password=credential_postgres_dbuser.password
    )
    ...

When collecting the test, the credential_postgres_dbuser instructs pytest-keyring to fetch the credential for the "postgres" service and the "dbuser" username, by making the following call to keyring.get_credential:

keyring.get_credential("postgres", "dbuser")

Configuring a keyring backend

The --keyring-backend configuration flag can be used to specify a keyring backend. For example, setting the null keyring backend:

pytest --keyring-backend=keyring.backends.null.Keyring

Causes all credentials and passwords to be None:

def test_with_null_backend(credential_postgres_dbuser, password_postgres_dbuser):
    assert credential_postgres_dbuser is None
    assert password_postgres_dbuser is None
    ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_keyring-1.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_keyring-1.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_keyring-1.1.0.tar.gz.

File metadata

  • Download URL: pytest_keyring-1.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.7 Linux/6.11.8-arch1-2

File hashes

Hashes for pytest_keyring-1.1.0.tar.gz
Algorithm Hash digest
SHA256 cb468d4d04552b599a2f081ea84b9a8d06a39c7b49563fcb7f4bee93c126e439
MD5 7637a104d4e7b5b7ac1c9441537689c9
BLAKE2b-256 7d1208451c2a0758d094bb5dc115ddbb26805321eae39a65b567df9716cc8a39

See more details on using hashes here.

File details

Details for the file pytest_keyring-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_keyring-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.7 Linux/6.11.8-arch1-2

File hashes

Hashes for pytest_keyring-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0acd7ce8e8ec0c7af38c03406aa616a636f4f9c088d5ce91e25a57b6f8863693
MD5 601efe04faf90d2e4dd5a4b20759a659
BLAKE2b-256 e25eb731ed09cc88af56c346b8f7eb81326857d02ed3abbfb2b4d585a557e429

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page