Skip to content

Commit 8ed3c23

Browse files
authored
Merge pull request #70 from nschloe/skip-expected-output
Skip expected output
2 parents 8e20f3e + b20e114 commit 8ed3c23

File tree

4 files changed

+111
-6
lines changed

4 files changed

+111
-6
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Conditionally skipping code blocks works with `skipif`, e.g.,
6767
<!--pytest-codeblocks:skipif(sys.version_info <= (3, 7))-->
6868
```
6969

70-
You can also skip all blocks in the entire file by putting
70+
Skip the entire file by putting
7171
```markdown
7272
<!--pytest-codeblocks:skipfile-->
7373
```
@@ -136,6 +136,9 @@ gives
136136
```
137137
````
138138

139+
(Conditionally) Skipping the output verfication works by prepending the first
140+
block with `skip`/`skipif` (see [above](#skipping-code-blocks)).
141+
139142
#### Expected errors
140143

141144
Some code blocks are expected to give errors. You can verify this with

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pytest-codeblocks
3-
version = 0.12.1
3+
version = 0.12.2
44
author = Nico Schlömer
55
author_email = [email protected]
66
description = Test code blocks in your READMEs

src/pytest_codeblocks/main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]:
9898
"Found <!--pytest-codeblocks-expected-output--> "
9999
+ "but block already has expected_output."
100100
)
101-
expected_output = "".join(code_block)
102-
out[-1] = CodeBlock(
103-
out[-1].code, out[-1].lineno, out[-1].syntax, expected_output
104-
)
101+
out[-1].expected_output = "".join(code_block)
105102
elif keyword == "cont":
106103
if len(out) == 0:
107104
raise RuntimeError(

tests/test_skipif.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
def test_skip(testdir):
2+
string = """
3+
Lorem ipsum
4+
5+
<!--pytest-codeblocks:skip-->
6+
7+
```python
8+
print(1 + 3)
9+
```
10+
"""
11+
testdir.makefile(".md", string)
12+
result = testdir.runpytest("--codeblocks")
13+
result.assert_outcomes(skipped=1)
14+
15+
16+
def test_skip_expected_output(testdir):
17+
string = """
18+
Lorem ipsum
19+
20+
<!--pytest-codeblocks:skip-->
21+
22+
```python
23+
print(1 + 3)
24+
```
25+
26+
<!--pytest-codeblocks:expected-output-->
27+
28+
```
29+
25abc
30+
```
31+
32+
"""
33+
testdir.makefile(".md", string)
34+
result = testdir.runpytest("--codeblocks")
35+
result.assert_outcomes(skipped=1)
36+
37+
38+
def test_skipif(testdir):
39+
string = """
40+
Lorem ipsum
41+
42+
<!--pytest-codeblocks:skipif(1 < 3)-->
43+
44+
```python
45+
print(1 + 3)
46+
```
47+
"""
48+
testdir.makefile(".md", string)
49+
result = testdir.runpytest("--codeblocks")
50+
result.assert_outcomes(skipped=1)
51+
52+
string = """
53+
Lorem ipsum
54+
55+
<!--pytest-codeblocks:skipif(1 > 3)-->
56+
57+
```python
58+
print(1 + 3)
59+
```
60+
"""
61+
testdir.makefile(".md", string)
62+
result = testdir.runpytest("--codeblocks")
63+
result.assert_outcomes(passed=1)
64+
65+
66+
def test_skipif_expected_output(testdir):
67+
string = """
68+
Lorem ipsum
69+
70+
<!--pytest-codeblocks:skipif(1 < 3)-->
71+
72+
```python
73+
print(1 + 3)
74+
```
75+
76+
<!--pytest-codeblocks:expected-output-->
77+
78+
```
79+
25abc
80+
```
81+
82+
"""
83+
testdir.makefile(".md", string)
84+
result = testdir.runpytest("--codeblocks")
85+
result.assert_outcomes(skipped=1)
86+
87+
string = """
88+
Lorem ipsum
89+
90+
<!--pytest-codeblocks:skipif(1 > 3)-->
91+
92+
```python
93+
print(1 + 3)
94+
```
95+
96+
<!--pytest-codeblocks:expected-output-->
97+
98+
```
99+
4
100+
```
101+
102+
"""
103+
testdir.makefile(".md", string)
104+
result = testdir.runpytest("--codeblocks")
105+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)