Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ assert captured.out == "hello\n"

As you can see above, the fixture value will be injected as a global. For `autouse=True` fixtures, the value is only injected as a global if it's explicitly added using a `fixture:<name>` marker.

### The source file path

Code fences receive a `__file__` global containing the path of the Markdown or
Python file where the fence was found. This makes it possible to load fixtures
stored next to a document:

````markdown
```python
from pathlib import Path

fixture = Path(__file__).with_name("fixture.json").read_text()
```
````

### Async Fixtures (pytest-asyncio)

If you have [pytest-asyncio](https://pypi.org/project/pytest-asyncio/) installed, you can use async fixtures with your markdown tests. The async fixture will be executed and its resolved value will be injected into the test code:
Expand Down
1 change: 1 addition & 0 deletions src/pytest_markdown_docs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def runtest(self):

mod = types.ModuleType("fence") # dummy module
all_globals = mod.__dict__
all_globals["__file__"] = str(self.test_definition.source_path)
for global_set in global_sets:
all_globals.update(global_set)

Expand Down
18 changes: 18 additions & 0 deletions tests/plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ def pytest_markdown_docs_globals():
result.assert_outcomes(passed=1, failed=2)


def test_file_global_points_to_markdown_source(testdir):
testdir.makefile(".txt", neighbor="from neighbor\n")
testdir.makefile(
".md",
test_file="""
```python
from pathlib import Path

assert Path(__file__).name == "test_file.md"
assert Path(__file__).with_name("neighbor.txt").read_text().strip() == "from neighbor"
```
""",
)

result = testdir.runpytest("--markdown-docs")
result.assert_outcomes(passed=1)


def test_continuation(testdir):
testdir.makefile(
".md",
Expand Down