Skip to main content

Pytest fixture to generate unique values.

Project description

Pytest fixture to generate unique values.

License PyPI Issues

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

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_unique-0.1.7.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

pytest_unique-0.1.7-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_unique-0.1.7.tar.gz.

File metadata

  • Download URL: pytest_unique-0.1.7.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.11.14 Linux/6.11.0-1018-azure

File hashes

Hashes for pytest_unique-0.1.7.tar.gz
Algorithm Hash digest
SHA256 7e48682ca5635e356454af92617994cd037717dc4ab7d8fec2d7695902e3a65f
MD5 4f3a8ee409c1ebe54002cc41744acf85
BLAKE2b-256 53230ec8691f6ddbdbd07181dd5a504a2c4ac94fa50364344780c716b19f23f9

See more details on using hashes here.

File details

Details for the file pytest_unique-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pytest_unique-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.11.14 Linux/6.11.0-1018-azure

File hashes

Hashes for pytest_unique-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b34eceb33590e14ad8db69b34c8534417f4bdf2c09845fc0e85ca720a7f2095c
MD5 e402102ab23302a58e106cf35ec54545
BLAKE2b-256 6eb58a9bb1e35ece7bda52826332df19b9f1926597ba83873c4e907a04ca7bdb

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