I'm using doctest to test code snippets in .rst files. In many cases, the most important tests are simple validity checks on yaml configs.
For example:
>>> my_yaml = """
... foo:
... bar:
... - baz1
... - baz2
... """
>>> my_yaml_dict = yaml.load(my_yaml_string, Loader=yaml.FullLoader)
>>> assert "baz_2" in my_yaml_dict["foo"]["bar"]
To make code more readable and copy-pasteable, I'd like to only display the yaml block, and put the test itself in a hidden code block.
This works:
>>> my_yaml = """
... foo:
... bar:
... - baz1
... - baz2
... """
.. invisible-code-block: python
>>> my_yaml_dict = yaml.load(my_yaml_string, Loader=yaml.FullLoader)
>>> assert "baz_2" in my_yaml_dict["foo"]["bar"]
However, the yaml block is still encapsulated in my_yaml="""...""", which makes it hard to copy-paste.
Is there a way to treat a code-block itself as a variable? I'm imagining something along these lines:
.. code-block:: yaml # doctest: +varname=my_yaml_string
foo:
bar:
- baz1
- baz2
.. invisible-code-block: python
>>> my_yaml_dict = yaml.load(my_yaml_string, Loader=yaml.FullLoader)
>>> assert "baz_2" in my_yaml_dict["foo"]["bar"]