Working with module_globals

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

Nobody has claimed this yet.

Assessment

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

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.

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!

Dominant language
Python
Stars
181
Forks
29
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from pydantic/pytest-examples

All issues in pydantic/pytest-examples

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.