Deterministic Fixture Factory

Generate repeatable fake users from a seeded random source, with per-call overrides and stable identifiers for snapshots.

PythonCli Tooling

Code

pythonsnippet
51 lines
import random
import string
from dataclasses import dataclass, replace


@dataclass(frozen=True)
class User:
    identifier: int
    email: str
    display_name: str
    created_at: str


class FixtureFactory:
    def __init__(self, seed: int = 20261021) -> None:
        self._rng = random.Random(seed)
        self._next_id = 1
        self._created_at = "2026-10-21T00:00:00Z"

    def _word(self, length: int = 6) -> str:
        return "".join(self._rng.choice(string.ascii_lowercase) for _ in range(length))

    def user(self, **overrides: object) -> User:
        identifier = self._next_id
        self._next_id += 1
        user = User(
            identifier=identifier,
            email=f"{self._word(5)}.{identifier}@example.test",
            display_name=f"{self._word(4).title()} {self._word(6).title()}",
            created_at=self._created_at,
        )
        return replace(user, **overrides)  # type: ignore[arg-type]

    def users(self, count: int) -> list[User]:
        if count < 0:
            raise ValueError("count must not be negative")
        return [self.user() for _ in range(count)]


def main() -> None:
    first = FixtureFactory(seed=7).users(3)
    again = FixtureFactory(seed=7).users(3)
    other = FixtureFactory(seed=8).users(3)
    print(first)
    print("deterministic:", first == again)
    print("seed changes output:", first != other)
    print("override:", replace(first[0], email="qa@example.test"))


if __name__ == "__main__":
    main()

Output

Captured from a sandboxed run of this snippet in the build pipeline.

[User(identifier=1, email='kemub.1@example.test', display_name='Crdl Sbqgbc', created_at='2026-10-21T00:00:00Z'), User(identifier=2, email='nnchc.2@example.test', display_name='Rnbs Dhuusb', created_at='2026-10-21T00:00:00Z'), User(identifier=3, email='ssmbh.3@example.test', display_name='Brej Nerdsj', created_at='2026-10-21T00:00:00Z')]
deterministic: True
seed changes output: True
override: User(identifier=1, email='qa@example.test', display_name='Crdl Sbqgbc', created_at='2026-10-21T00:00:00Z')

Inside the snippet

Count the classes User and FixtureFactory and the functions __init__, _word, user and users as the entire surface area of Deterministic Fixture Factory.

Failure is loud: ValueError aborts the current operation instead of degrading it.

The result is a fresh construction, so callers can keep their originals.

What to watch for

  • The guard count < 0 short-circuits the flow with an early raise when its condition holds.

Using it

The primary signature is def user(self, **overrides: object) -> User

Running the file prints labeled results such as deterministic:, seed changes output:, override: — each operation’s effect is visible in the output.

main calls 1 helper in sequence: users.

Key statements

def __init__(self, seed: int = 20261021) -> None:
    self._rng = random.Random(seed)
    self._next_id = 1
    self._created_at = "2026-10-21T00:00:00Z"
def users(self, count: int) -> list[User]:
    if count < 0:
        raise ValueError("count must not be negative")
    return [self.user() for _ in range(count)]
def _word(self, length: int = 6) -> str:
    return "".join(self._rng.choice(string.ascii_lowercase) for _ in range(length))

Interactive Tools for This Topic

FAQ

Does `user` modify its input?
It does not. Inputs are only read; the return value is freshly built.
How does the snippet fail?
The code refuses to continue: `ValueError` is thrown while the state is still recoverable.
What does Deterministic Fixture Factory print when run?
No setup needed: run the file and look for labeled lines like `deterministic:`, `seed changes output:`, `override:` in the output.