Skip to content

Is there a way to test a later code example that relies on an earlier one? #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
robbie-portia opened this issue Feb 10, 2025 · 1 comment

Comments

@robbie-portia
Copy link

This is an awesome library - thanks for creating and maintaining it!

If I have a markdown doc that has 2 code snippets, is there any way to mark that the second one relies on the first one and therefore, in order to test the second example you need to take the code from the first example too?

E.g. test.md

You can import environment variables like this:

import os
s.environ.get("MY_ENV_VAR")

And you can also specify a default value:

s.environ.get("MY_ENV_VAR", "default")
@robbie-portia
Copy link
Author

Just coming back to this, I found there is a way to do this within the current setup. If you add depends_on=<> as a tag to one example, id=<> to another example and then have the following in your run code, it works. It's a little hacky and would be nicer if it were packaged better, but does the job 😃

@pytest.mark.parametrize("example", find_examples("docs/product/"), ids=str)
def test_docstrings(example: CodeExample, eval_example: EvalExample):
    # For example, if you have depends_on=example1, then we'll look for an example with
    # id=example1 and load that code in before the current example.
    dependent_examples = [
        _get_example_by_id(tag.split("depends_on=")[1])
        for tag in example.prefix_tags()
        if "depends_on=" in tag
    ]
    example.source = "\n".join(
        [dependent_example.source for dependent_example in dependent_examples]
        + [example.source]
    )

    eval_example.run(example)

def _get_example_by_id(id: str) -> CodeExample:
    for example in find_examples("docs/product/"):
        if f"id={id}" in example.prefix_tags():
            return example
    raise ValueError(f"Example with id {id} not found")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant