Skip to content

Commit

Permalink
Support pathlib.Path arguments to find_examples (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu authored Mar 30, 2023
1 parent 4c9173d commit 03f7fad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pytest_examples/find_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __str__(self):
return f'{path}:{self.start_line}-{self.end_line}'


def find_examples(*paths: str, skip: bool = False) -> Iterable[CodeExample]:
def find_examples(*paths: str | Path, skip: bool = False) -> Iterable[CodeExample]:
"""
Find Python code examples in markdown files and python file docstrings.
Expand Down
25 changes: 18 additions & 7 deletions tests/test_find_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,35 @@ def test_find_md_example(pytester: pytest.Pytester):
pytester.makepyfile(
# language=Python
"""
from pathlib import Path
from pytest_examples import find_examples
import pytest
@pytest.mark.parametrize('example', find_examples('.'), ids=str)
def test_find_examples(example):
def test_find_examples_str(example):
assert example.indent == 0
assert example.end_line == example.start_line + 2
@pytest.mark.parametrize('example', find_examples(Path('.')), ids=str)
def test_find_examples_path(example):
assert example.indent == 0
assert example.end_line == example.start_line + 2
"""
)

result = pytester.runpytest('-p', 'no:pretty', '-v')
output = '\n'.join(result.outlines)
result.assert_outcomes(passed=4)

assert 'test_find_examples[my_file.md:3-5] PASSED' in output
assert 'test_find_examples[my_file.md:7-9] PASSED' in output
assert 'test_find_examples[my_file.md:11-13] PASSED' in output
assert 'test_find_examples[my_file.md:15-17] PASSED' in output
result.assert_outcomes(passed=8)

assert 'test_find_examples_str[my_file.md:3-5] PASSED' in output
assert 'test_find_examples_str[my_file.md:7-9] PASSED' in output
assert 'test_find_examples_str[my_file.md:11-13] PASSED' in output
assert 'test_find_examples_str[my_file.md:15-17] PASSED' in output
assert 'test_find_examples_path[my_file.md:3-5] PASSED' in output
assert 'test_find_examples_path[my_file.md:7-9] PASSED' in output
assert 'test_find_examples_path[my_file.md:11-13] PASSED' in output
assert 'test_find_examples_path[my_file.md:15-17] PASSED' in output


def test_find_py_example(pytester: pytest.Pytester):
Expand Down

0 comments on commit 03f7fad

Please sign in to comment.