Skip to main content

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.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 fhir-sdc-testkit 3.0.0
File Size Uploaded Provenance Actions
fhir_sdc_testkit-3.0.0.tar.gz 126.2 kB With provenance Details for fhir_sdc_testkit-3.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 135.8 kB

Release files / fhir_sdc_testkit-3.0.0.tar.gz

Download URL fhir_sdc_testkit-3.0.0.tar.gz
Size 126.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2c843411ebc1b98d3c905e0ba37a74a1ed4db8c8c0a1ebaea66338a66cceeff8
BLAKE2b-256 checksum
How to use checksums
aeeac9aeab2dc3360e5706c4c0c370a7358a4093edaec3dc907b8237fb837fe6
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 23, 2026.

Transparency log

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

Download URL fhir_sdc_testkit-3.0.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77bb88abae361219fe206f6796a63bed57f7aa036d6a42b1ed2fdf2cdcedd29f
BLAKE2b-256 checksum
How to use checksums
032c37f55114b24a01eccde4f186eca07084d54c5ef219433ea3f844a425abb0
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 23, 2026.

Transparency log

Security for fhir-sdc-testkit 3.0.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 fhir_sdc_testkit-3.0.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 23, 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