pydantic / pydantic/pytest-examples

Working with module_globals

Open
#56 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
178
Forks
25
Avg merge
4d 1h
Merged PRs (30d)
7

Description

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!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure from the issue's parametrized test using the docs/**/*.md examples and the module_globals passed to run_print_update and run_print_check. Trace those entry points to determine why datadir and foo are unavailable, then verify that both assertions pass in update and check modes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.