Skip to main content

pytest-clerk-mock

A pytest plugin for mocking Clerk auth plus the supported Clerk backend SDK clients.

Supported SDK surfaces:

  • users
  • organizations
  • organization_memberships

The plugin patches Clerk at the SDK level, so it works even if your app instantiates its Clerk client before the test runs.

Installation

pip install pytest-clerk-mock

Or with Poetry:

poetry add --group dev pytest-clerk-mock

Quick Start

def test_create_user(mock_clerk):
    user = mock_clerk.users.create(
        email_address=["test@example.com"],
        first_name="John",
        last_name="Doe",
    )

    fetched = mock_clerk.users.get(user_id=user.id)

    assert fetched.email_addresses[0].email_address == "test@example.com"

Async methods use Clerk-style signatures too:

async def test_create_user_async(mock_clerk):
    user = await mock_clerk.users.create_async(email_address=["test@example.com"])
    fetched = await mock_clerk.users.get_async(user_id=user.id)

    assert fetched.id == user.id

Auth Helpers

def test_auth(mock_clerk, request, options):
    mock_clerk.configure_auth("user_123", org_id="org_456", org_role="org:admin")

    result = mock_clerk.authenticate_request(request, options)

    assert result.is_signed_in

Temporary auth context:

with mock_clerk.as_user("user_456", org_id="org_789"):
    ...

Predefined users:

  • MockClerkUser.TEAM_OWNER
  • MockClerkUser.TEAM_MEMBER
  • MockClerkUser.GUEST
  • MockClerkUser.UNAUTHENTICATED

Common Examples

Organizations:

created = mock_clerk.organizations.create(
    request={
        "name": "My Organization",
        "created_by": "user_123",
        "slug": "my-org",
    }
)

org = mock_clerk.organizations.get(organization_id=created.id)

Organization memberships:

membership = mock_clerk.organization_memberships.create(
    organization_id="org_123",
    user_id="user_123",
    role="org:admin",
)

If your app reads memberships through users.get_organization_memberships(...), seed them explicitly:

from pytest_clerk_mock import MockOrganizationMembershipsResponse

mock_clerk.users.set_organization_memberships(
    "user_123",
    MockOrganizationMembershipsResponse(data=[], total_count=0),
)

Scope

This package aims for strong parity for the supported Clerk SDK surfaces above. It does not try to mock every Clerk API domain.

The contract tests and CI job are the source of truth for supported method and model parity.

Test Helpers

Useful non-SDK helpers:

  • mock_clerk.users.set_organization_memberships(...)
  • mock_clerk.organizations.add(...)
  • mock_clerk.organization_memberships.get(...)

Custom fixture:

from pytest_clerk_mock import create_mock_clerk_fixture

mock_clerk = create_mock_clerk_fixture(
    default_user_id="user_custom",
    default_org_id="org_custom",
    default_org_role="org:member",
    autouse=True,
)

Context manager:

from pytest_clerk_mock import mock_clerk_backend

with mock_clerk_backend(default_user_id="user_123") as mock:
    mock.configure_auth("user_456")

Low-level patch helpers:

from pytest_clerk_mock import (
    create_clerk_errors,
    mock_clerk_user_creation,
    mock_clerk_user_creation_failure,
    mock_clerk_user_exists,
)

License

MIT

Metadata

Release files for pytest-clerk-mock 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-clerk-mock 0.6.0
File Size Uploaded Provenance Actions
pytest_clerk_mock-0.6.0.tar.gz 23.2 kB With provenance Details for pytest_clerk_mock-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-clerk-mock 0.6.0
File Interpreter ABI Platform Actions
pytest_clerk_mock-0.6.0-py3-none-any.whl Python 3 none any Details for pytest_clerk_mock-0.6.0-py3-none-any.whl

Total release size: 53.5 kB

Release files / pytest_clerk_mock-0.6.0.tar.gz

Download URL pytest_clerk_mock-0.6.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
197f8c4f4edeeeec052acd423275b7a7fbbff90e58037732d9d78cefc6974388
BLAKE2b-256 checksum
How to use checksums
1badee6eabff5b674b39992c129b27f4b41889bc81167a1842c5760de98f247f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / pytest_clerk_mock-0.6.0-py3-none-any.whl

Download URL pytest_clerk_mock-0.6.0-py3-none-any.whl
Size 30.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e759fd7890bfe0f97a816b5effd84813fe52b5cde433099d808ae59e0021ff03
BLAKE2b-256 checksum
How to use checksums
218ee27fe1962adf323dea32375ae11932016a12f7e750452837c857284f8daa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for pytest-clerk-mock 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytest_clerk_mock-0.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page