Skip to content
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

Working with module_globals #56

Open
TiemenSch opened this issue Feb 14, 2025 · 1 comment
Open

Working with module_globals #56

TiemenSch opened this issue Feb 14, 2025 · 1 comment

Comments

@TiemenSch
Copy link

TiemenSch commented Feb 14, 2025

So i tried to inject some fixtures as "module_globals" into my doctests, but I can't seem to get it to work.

I've tried the following:

from pathlib import Path

import pytest
from pytest_examples import CodeExample, EvalExample, find_examples

DOCS_DIR = Path(__file__).parent.parent / "docs"
DOCS_FILES = DOCS_DIR.glob("**/*.md")

@pytest.mark.parametrize("example", find_examples(*DOCS_FILES), ids=str)
def test_docs(
    example: CodeExample,
    eval_example: EvalExample,
    datadir: Path,
):
    """Test the package's documentation."""

    globals = dict(datadir=datadir, foo=2)

    eval_example.set_config(line_length=88)
    if eval_example.update_examples:
        eval_example.format(example)
        eval_example.run_print_update(example, module_globals=globals)
    else:
        eval_example.lint(example)
        eval_example.run_print_check(example, module_globals=globals)

where my example Markdown file is picked up fine in the documentation directory, but the examples

assert datadir

and

assert foo == 2

both fail.

I can't find documentation on the feature, but would love to know how it's supposed to work!

@TiemenSch
Copy link
Author

Ah, it turned out that ruff's formatting failed on variables being undefined. Adding

eval_example.set_config(ruff_ignore=["F821"])

did wonders already

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