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
Built Distribution
Hashes for pytest_keyring-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 487d2ac10d3d646995cddbb807545e66b62470eacdf9c00d9dfb24d5371eab34 |
|
MD5 | 6fe4c96e2a46259e635277a810f7cd9a |
|
BLAKE2b-256 | 0fd907349d51d088a4e9c5c0b940d50cae3d5aa3115e523007c1c9e02aeb478c |