Skip to content

Using YAML#

For handling very complex objects, especially in the context of end-to-end (E2E) tests, it can be highly beneficial to store expectations in separate YAML files. This module provides helper methods to both dump expectations into a YAML string and load them from a YAML string.

Any expectation supported by this module can be converted into YAML using the expectation_to_yaml function. Let's take a look at an example:

Python
expectation = ObjectAttributes(
    {
        'created_at': Stringified('2020-06-08 12:30:00'),
        'books': Unordered(
            [
                ObjectAttributes(
                    {
                        'name': 'Otworzyć po mojej śmierci',
                        'authors': ['Abelard Giza'],
                        'translations': MappingSubset({}),
                    },
                ),
                ObjectAttributes(
                    {
                        'name': 'Hobbit',
                        'authors': Stringified("['J. R. R. Tolkien']"),
                        'language': 'en',
                    },
                ),
            ],
        ),
    },
)

yaml_string = expectation_to_yaml(expectation)

The resulting yaml_string will have the following content:

YAML
!ObjectAttributes
books: !Unordered
- !ObjectAttributes
  authors:
  - Abelard Giza
  name: "Otworzy\u0107 po mojej \u015Bmierci"
  translations: !MappingSubset {}
- !ObjectAttributes
  authors: !Stringified '[''J. R. R. Tolkien'']'
  language: en
  name: Hobbit
created_at: !Stringified '2020-06-08 12:30:00'

You can also load a YAML string back into expectation objects using the expectation_from_yaml function:

Python
expectation = expectation_from_yaml(yaml_string)

Keep in mind that the loaded expectation may not be exactly identical to the original one before dumping, but it will be functionally equivalent.