Pytest fixture to generate unique values.
Project description
Pytest fixture to generate unique values.
Requirements
You will need the following prerequisites to use pytest-unique:
Python 3.9, 3.10, 3.11, 3.12, 3.13
Installation
To install pytest-unique:
$ pip install pytest-unique
Usage
You can use the unique fixture by passing it as a test argument:
def test_integer(unique):
assert unique("integer") > 0
Here are some of the plugins available by default:
bytes with invalid UTF8 characters.
digits with just numbers.
email with a unique username.
float with a whole part and a decimal part.
integer with base and mod arguments.
password with lowercase, uppercase, digits and punctuation arguments.
text with prefix, suffix and separator arguments.
Extensions
The unique fixture can be extended with custom plugins:
from datetime import datetime, timezone
def unique_datetime(unique)
"""Return a datetime unique to this factory instance."""
timestamp = unique("integer")
return datetime.fromtimestamp(timestamp, timezone.utc)
Then, add it to the pyproject.toml file of your project:
[tool.poetry.plugins."pytest_unique.unique"]
datetime = "your_project.unique:unique_datetime"
You can then use the plugin like all other plugins:
from datetime import datetime
def test_datetime(unique):
assert isinstance(unique("datetime"), datetime)
Limitations
The unique fixture cannot be used as parameters in parameterized tests by virtue of being a fixture. The reason it is a fixture rather than a function is that unique values are generated from an instance of a counter that returns sequential values. This makes it possible to see the order in which values were generated which can be useful when troubleshooting.
Resources
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file pytest_unique-0.1.2.tar.gz
.
File metadata
- Download URL: pytest_unique-0.1.2.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31b64c5c3d5c0f857865f6f4230102a6fb366468ec714a99cbaec969cc13aa1f |
|
MD5 | d4943ea6a83cee392941dd5b124a57cb |
|
BLAKE2b-256 | 9402f3fb043d6e3b2b541c858e9cd67f9acb7112967cea4c273a0aff471326c3 |
File details
Details for the file pytest_unique-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: pytest_unique-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f18d04124d70507514387897fc0ed250269ffff7af4547729ca9b741f30ea7ac |
|
MD5 | e8dc6fad836a47dc02c1074c62a8a1e2 |
|
BLAKE2b-256 | 6ae2687cebe2fe070714b880662dd67e2e795cc8339ebe108975a9be2ef5bf97 |