Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

fhir-sdc-testkit

Test an SDC Questionnaire and its extraction the way a form is really filled: $populate runs, answers are placed at each linkId's nesting, then QuestionnaireResponse/$extract runs. With submit=True the QuestionnaireResponse is saved before extraction and the bundle is submitted after it. Only what a user types (or the frontend calculates) is faked; everything else runs on a real server implementing the SDC operations, such as fhir-sdc.

pip install fhir-sdc-testkit

It is released from the fhir-sdc repository, with the same version as the fhir-sdc release it is tested against.

The client

Everything takes an AsyncFHIRClient rooted at the server's /fhir base, built with dump_resource — fhirpy cannot serialize pydantic resources without it:

fhir_client = AsyncFHIRClient(
    f"{base_url}/fhir",
    authorization=authorization,
    dump_resource=lambda resource: resource.model_dump(),
)

Usage

import fhirpy_types_r4b as r4b
from fhir_sdc_testkit import answer, launch_context, populate_and_extract


async def test_intake_records_the_address(fhir_client):
    patient = await fhir_client.create(r4b.Patient(name=[r4b.HumanName(family="Test")]))

    await populate_and_extract(
        fhir_client,
        "patient-intake",
        parameters=launch_context(
            {"Patient": patient}, subject=r4b.Reference(reference=f"Patient/{patient.id}")
        ),
        answers={"postal-code": [answer(valueString="75001")]},
        submit=True,
    )

    updated = await fhir_client.get(r4b.Patient, patient.id)
    assert updated.address[0].postalCode == "75001"

populate_and_extract returns the QuestionnaireResponse and the bundle $extract returned (None when there is nothing to extract). Without submit neither is saved, so the bundle can be asserted on directly. With it, the response is the saved one — saved first, since a Mapping may reference it by id — and the bundle has been submitted.

populate and extract run the two operations on their own and save nothing.

The Questionnaire

populate_and_extract and populate take an r4b.Questionnaire, saved or not (an assembled one, for example), or a string, found by canonical (url|version or url) and then by id. A string matching several versions or nothing raises LookupError. $populate and $extract always receive the resource itself.

Answers

Keys are linkIds, addressed flatly — groups in between are created for you:

answers={
    "country": [answer(valueCoding=r4b.Coding(code="FR"))],
    "postal-code": [answer(valueString="75001")],
}

A repeating group takes one answer set per occurrence, filled positionally over the occurrences $populate produced, appending any that are missing and leaving extra populated ones alone:

answers={
    "contacts": [
        {"phone": [answer(valueString="+1 555 0100")], "label": [answer(valueString="home")]},
        {"phone": [answer(valueString="+1 555 0111")], "label": [answer(valueString="work")]},
    ],
}

Inside a set, linkIds are flat again. A linkId that does not belong to the group raises, naming it.

Parameters

parameters is required: it is passed to $populate and merged into the $extract call, so anything the server accepts can go in it. launch_context() builds the SDC $populate input — a resource or a Reference under each context name, and the optional subject. Pass at least one of them: a $populate reply that is not Parameters raises ValueError.

parameters=launch_context({"Patient": patient, "Author": r4b.Reference(reference="Practitioner/1")})

A list under a name builds one context with a content part per value, as SDC defines it. fhir-sdc uses only the first content for now.

Failures

An error response from the server raises fhirpy's exception for its status — OperationOutcome for most 4xx and 5xx responses. A violated itemConstraint the server checks during $extract and a rejected transaction both arrive this way; they differ in issue[].code. fhir-sdc reports a constraint's key, and a server's own validation code, such as invalid, for a transaction. An error or fatal issue in the $extract result and, with submit, a batch entry the server rejects raise ExtractionError.

with pytest.raises(OperationOutcome) as refused:
    await populate_and_extract(fhir_client, "patient-intake", parameters=parameters, answers=answers)
assert refused.value.resource["issue"][0]["code"] == "postal-code-required"

Permissions

The client's credentials need these requests, relative to its base URL:

Request When
GET Questionnaire?url=…, GET Questionnaire?_id=… the Questionnaire is passed as a string
POST Questionnaire/$populate populate, populate_and_extract
POST QuestionnaireResponse/$extract extract, populate_and_extract
POST QuestionnaireResponse populate_and_extract with submit
POST / (the extracted transaction or batch) populate_and_extract with submit

With submit, the bundle's entries also need the access their own requests do, such as POST Patient. The operations may need more: fhir-sdc runs them with the caller's credentials, reading the subject and Reference context values, x-fhir-query expressions, source queries (as a POST / batch) and, in $extract, the Mappings.

Development

uv sync
uv run pytest
uv run ruff format . && uv run ruff check . && uv run mypy

That suite runs without a server. $populate, constraints and extraction are covered against a running Aidbox and fhir-sdc by tests/testkit/ in the repository root — ./run_test.sh tests/testkit.

Metadata

Release files for fhir-sdc-testkit 3.0.0a2

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

Source distribution (sdist)

Source distribution for fhir-sdc-testkit 3.0.0a2
File Size Uploaded Provenance Actions
fhir_sdc_testkit-3.0.0a2.tar.gz 126.2 kB With provenance Details for fhir_sdc_testkit-3.0.0a2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fhir-sdc-testkit 3.0.0a2
File Interpreter ABI Platform Actions
fhir_sdc_testkit-3.0.0a2-py3-none-any.whl Python 3 none any Details for fhir_sdc_testkit-3.0.0a2-py3-none-any.whl

Total release size: 135.9 kB

Release files / fhir_sdc_testkit-3.0.0a2.tar.gz

Download URL fhir_sdc_testkit-3.0.0a2.tar.gz
Size 126.2 kB
Tags Source
SHA-256 checksum
How to use checksums
729974dab936504c558c4ba5d4a9fda1a352745feeb0a6a51b784e49d1c59ce2
BLAKE2b-256 checksum
How to use checksums
003c398ee03b61277c90f13c269cfa950787c98b2a6d9cd615436ad354fc188b
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 Oct 27, 2026.

Transparency log

Release files / fhir_sdc_testkit-3.0.0a2-py3-none-any.whl

Download URL fhir_sdc_testkit-3.0.0a2-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c5947b486de70695fee1088e258f4d30a066f02e68d7aa8cce9b6a1e4314e07
BLAKE2b-256 checksum
How to use checksums
6ac96c9181e2d421c3fe4f91a0aba625ce05fde0766e039663f2427307f2ddfd
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 Oct 27, 2026.

Transparency log

Security for fhir-sdc-testkit 3.0.0a2

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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fhir_sdc_testkit-3.0.0a2.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 Oct 27, 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