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"]
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
Release files for pytest-unique 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest_unique-0.1.8.tar.gz | 60.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_unique-0.1.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 69.1 kB
Release files / pytest_unique-0.1.8.tar.gz
| Download URL | pytest_unique-0.1.8.tar.gz |
|---|---|
| Size | 60.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
21111b237edd0cb49667f0e8cc09ca3bb3d4603420bf440d5dbd9570ae945e99
|
|
BLAKE2b-256 checksum How to use checksums |
57fcbe0df3f8a03659cdfa8978a0289d447fef8424966a279fe3151f56c08af4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / pytest_unique-0.1.8-py3-none-any.whl
| Download URL | pytest_unique-0.1.8-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
20edb991b5b251e47b25a73a72a0ad5a220bff7dafd89fbe3c71eccfdd8737d1
|
|
BLAKE2b-256 checksum How to use checksums |
c19e4722dd00f1fc2d3d497cbf629f5158d1988d0e5230a8dd8b7cfd49a44ed5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|