Skip to content

Commit

Permalink
Merge pull request #70 from nschloe/skip-expected-output
Browse files Browse the repository at this point in the history
Skip expected output
  • Loading branch information
nschloe authored Dec 21, 2021
2 parents 8e20f3e + b20e114 commit 8ed3c23
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Conditionally skipping code blocks works with `skipif`, e.g.,
<!--pytest-codeblocks:skipif(sys.version_info <= (3, 7))-->
```

You can also skip all blocks in the entire file by putting
Skip the entire file by putting
```markdown
<!--pytest-codeblocks:skipfile-->
```
Expand Down Expand Up @@ -136,6 +136,9 @@ gives
```
````

(Conditionally) Skipping the output verfication works by prepending the first
block with `skip`/`skipif` (see [above](#skipping-code-blocks)).

#### Expected errors

Some code blocks are expected to give errors. You can verify this with
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pytest-codeblocks
version = 0.12.1
version = 0.12.2
author = Nico Schlömer
author_email = nico.schloemer@gmail.com
description = Test code blocks in your READMEs
Expand Down
5 changes: 1 addition & 4 deletions src/pytest_codeblocks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
"Found <!--pytest-codeblocks-expected-output--> "
+ "but block already has expected_output."
)
expected_output = "".join(code_block)
out[-1] = CodeBlock(
out[-1].code, out[-1].lineno, out[-1].syntax, expected_output
)
out[-1].expected_output = "".join(code_block)
elif keyword == "cont":
if len(out) == 0:
raise RuntimeError(
Expand Down
105 changes: 105 additions & 0 deletions tests/test_skipif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
def test_skip(testdir):
string = """
Lorem ipsum
<!--pytest-codeblocks:skip-->
```python
print(1 + 3)
```
"""
testdir.makefile(".md", string)
result = testdir.runpytest("--codeblocks")
result.assert_outcomes(skipped=1)


def test_skip_expected_output(testdir):
string = """
Lorem ipsum
<!--pytest-codeblocks:skip-->
```python
print(1 + 3)
```
<!--pytest-codeblocks:expected-output-->
```
25abc
```
"""
testdir.makefile(".md", string)
result = testdir.runpytest("--codeblocks")
result.assert_outcomes(skipped=1)


def test_skipif(testdir):
string = """
Lorem ipsum
<!--pytest-codeblocks:skipif(1 < 3)-->
```python
print(1 + 3)
```
"""
testdir.makefile(".md", string)
result = testdir.runpytest("--codeblocks")
result.assert_outcomes(skipped=1)

string = """
Lorem ipsum
<!--pytest-codeblocks:skipif(1 > 3)-->
```python
print(1 + 3)
```
"""
testdir.makefile(".md", string)
result = testdir.runpytest("--codeblocks")
result.assert_outcomes(passed=1)


def test_skipif_expected_output(testdir):
string = """
Lorem ipsum
<!--pytest-codeblocks:skipif(1 < 3)-->
```python
print(1 + 3)
```
<!--pytest-codeblocks:expected-output-->
```
25abc
```
"""
testdir.makefile(".md", string)
result = testdir.runpytest("--codeblocks")
result.assert_outcomes(skipped=1)

string = """
Lorem ipsum
<!--pytest-codeblocks:skipif(1 > 3)-->
```python
print(1 + 3)
```
<!--pytest-codeblocks:expected-output-->
```
4
```
"""
testdir.makefile(".md", string)
result = testdir.runpytest("--codeblocks")
result.assert_outcomes(passed=1)

0 comments on commit 8ed3c23

Please sign in to comment.