Skip to main content

JSON support for pytest-loco

Project description

pytest-loco-json

JSON extension for pytest-loco.

The pytest-loco-json extension adds first-class JSON support to the pytest-loco DSL. It provides facilities for decoding, encoding, and querying JSON data as part of test execution.

This extension is designed to integrate seamlessly with the pytest-loco plugin system and can be enabled by registering the json plugin. Once enabled, JSON becomes a native data format within the DSL, suitable for validation, transformation, and data-driven testing scenarios.

Encode

The dump feature serializes a value from the execution context into a JSON string using the high-performance orjson backend.

Encoding is typically used when preparing request payloads, exporting structured data, or normalizing values for comparison. The encoder accepts an optional sortKeys parameter that controls serialization behavior and enables deterministic key ordering.

Encoded values are returned as UTF-8 JSON strings and can be passed directly to actions (for example, HTTP requests) or stored in the execution context.

For example:

---
spec: case
title: Example of encoding a value
vars:
  value:
    name: Molecule Man
    secretIdentity: Dan Jukes
    age: 29

---
action: pass
export:
  jsonText: !dump
    source: !var value
    format: json

The result of executing this case will be the assignment of the following value (without indentation) to the variable jsonText:

{
  "name": "Molecule Man",
  "age": 29,
  "secretIdentity": "Dan Jukes"
}

Optionally, you can use the sortKeys option:

...
action: pass
export:
  jsonText: !dump
    source: !var value
    format: json
    sortKeys: yes

In this case, the keys will be sorted alphabetically:

{
  "age": 29,
  "name": "Molecule Man",
  "secretIdentity": "Dan Jukes"
}

Decode

The load feature parses a JSON string into native Python objects and stores them as values in the execution context.

Decoding allows JSON payloads to become fully addressable within the DSL execution context. Once decoded, values can be inspected, exported, validated, or transformed by subsequent steps.

Decoder parameters are intentionally minimal: decoding focuses on correctness and performance, while transformation logic is handled separately.

For example:

---
spec: case
title: Example of decoding a value

---
action: pass
title: Read JSON content from file
export:
  jsonText: !textFile test.json

---
action: pass
title: Try to decode JSON
export:
  jsonValue: !load
    source: !var jsonText
    format: json

The result of executing this case will be the assignment of the decoded JSON object to the variable jsonValue.

Transform by JSONPath

The load feature can be extended with an optional transformer that enables extracting data from decoded JSON structures using JSONPath expressions.

Transforms are applied after decoding and allow selecting either a single value or multiple values from complex, deeply nested documents. Behavior is configurable to return the first match, the last match, or a full list of matches.

This makes it possible to work with large or variable JSON payloads without hard-coding structural assumptions into the test logic.

Use the following test.json file contents as an example:

[
  {
    "name": "Molecule Man",
    "age": 29,
    "secretIdentity": "Dan Jukes",
    "powers": [
      "Radiation resistance",
      "Turning tiny",
      "Radiation blast"
    ]
  },
  {
    "name": "Madame Uppercut",
    "age": 39,
    "secretIdentity": "Jane Wilson",
    "powers": [
      "Million tonne punch",
      "Damage resistance",
      "Superhuman reflexes"
    ]
  }
]

Optionally, you can use the query transformer with load:

...

action: pass
title: Decode JSON and select the first match
export:
  jsonValue: !load
    source: !var jsonText
    format: json
    query: '$[*].name'
expect:
- title: Check first selected name
  value: !var jsonValue
  match: Molecule Man

By default, the first match of the JSONPath query is selected. This behavior can be controlled using the exactOne parameter (true or false; when false, the query returns a full list of matches) and the exactMode parameter (first or last on a single mode querying).

For example:

...

action: pass
title: Try to decode JSON and select all
export:
  jsonValue: !load
    source: !var jsonText
    format: json
    query: '$[?(@.age<30)].powers[*]'
    exactOne: no
expect:
- title: Check result is list of powers
  value: !var jsonValue
  match:
  - Radiation resistance
  - Turning tiny
  - Radiation blast

Inline querying

Inline querying allows JSONPath expressions to be defined directly inside the DSL using the dedicated !jsonpath instruction.

Inline queries are compiled during schema loading rather than at runtime, which improves error reporting and ensures invalid paths fail early. Compiled queries can be stored in variables and reused across steps.

This feature is especially useful for complex test suites where the same JSONPath expressions appear in multiple places, or when clarity and reuse are more important than brevity.

For example:

---
action: pass
title: Try to select from context by instruction
export:
  resultValues: !jsonpath jsonValue $[?(@.age<_.ageLimit)].powers[*]
expect:
- title: Check result is list of powers
  value: !var resultValues
  match:
  - Radiation resistance
  - Turning tiny
  - Radiation blast

The result is always a list.

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_loco_json-1.3.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

pytest_loco_json-1.3.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest_loco_json-1.3.1.tar.gz.

File metadata

  • Download URL: pytest_loco_json-1.3.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.14.0-1017-azure

File hashes

Hashes for pytest_loco_json-1.3.1.tar.gz
Algorithm Hash digest
SHA256 ede768a2d01d2b04be4126733c071d8f284d9cc3a94fea8a3eb985d148577ebd
MD5 977ec4acfc398d5a78857082692307db
BLAKE2b-256 000505e578a6d48e05f0b2b023c1458eff3320d0f9fb2979038502370e2a1573

See more details on using hashes here.

File details

Details for the file pytest_loco_json-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_loco_json-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.14.0-1017-azure

File hashes

Hashes for pytest_loco_json-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 345c018c14abbb6cacf734fb9418e894bed894c44e8c10281f0192f4d48614ad
MD5 f1a305c4278d8fdc275c73b703a58f04
BLAKE2b-256 83efa37e04416e7210dbec5aee9e1ed1e9122c0f870fbfefb7f07a5bc6013c1c

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