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.

Install

> pip install pytest-loco-json

Requirements:

  • Python 3.13 or higher

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: empty
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: empty
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: empty
title: Read JSON content from file
export:
  jsonText: !textFile test.json

---
action: empty
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: empty
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: empty
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.

For example:

---
action: empty
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.3.tar.gz (7.1 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.3-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_loco_json-1.3.3.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.11 Linux/6.11.0-1018-azure

File hashes

Hashes for pytest_loco_json-1.3.3.tar.gz
Algorithm Hash digest
SHA256 5ee88826c9e5ea1b65bfb44dcda6b48433504510a018b5e6e88758bba03388de
MD5 77b12665c59a18a2561450d03b42ca5a
BLAKE2b-256 dbeb36f66255fa0867eb1ef47535887c328b288e482a42fada68f8de40be2a20

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_loco_json-1.3.3-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.11 Linux/6.11.0-1018-azure

File hashes

Hashes for pytest_loco_json-1.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 117b80061b5927071fcb9d585ec563ab801e82a59e7abdc08e310fffe43a2fa5
MD5 4536f676f3fa48acd1844cc43f42b141
BLAKE2b-256 f9f4132ae7189a204768e7092a070ba4a1e62a8f3d4813feeaa2878dbd47ddba

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