diff --git a/README.md b/README.md index 8d7730a..32eeb1a 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Conditionally skipping code blocks works with `skipif`, e.g., ``` -You can also skip all blocks in the entire file by putting +Skip the entire file by putting ```markdown ``` @@ -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 diff --git a/setup.cfg b/setup.cfg index 86dedd6..c407897 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/src/pytest_codeblocks/main.py b/src/pytest_codeblocks/main.py index 78cc37e..c7debae 100644 --- a/src/pytest_codeblocks/main.py +++ b/src/pytest_codeblocks/main.py @@ -98,10 +98,7 @@ def extract_from_buffer(f, max_num_lines: int = 10000) -> list[CodeBlock]: "Found " + "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( diff --git a/tests/test_skipif.py b/tests/test_skipif.py new file mode 100644 index 0000000..e6efc7c --- /dev/null +++ b/tests/test_skipif.py @@ -0,0 +1,105 @@ +def test_skip(testdir): + string = """ + Lorem ipsum + + + + ```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 + + + + ```python + print(1 + 3) + ``` + + + + ``` + 25abc + ``` + + """ + testdir.makefile(".md", string) + result = testdir.runpytest("--codeblocks") + result.assert_outcomes(skipped=1) + + +def test_skipif(testdir): + string = """ + Lorem ipsum + + + + ```python + print(1 + 3) + ``` + """ + testdir.makefile(".md", string) + result = testdir.runpytest("--codeblocks") + result.assert_outcomes(skipped=1) + + string = """ + Lorem ipsum + + + + ```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 + + + + ```python + print(1 + 3) + ``` + + + + ``` + 25abc + ``` + + """ + testdir.makefile(".md", string) + result = testdir.runpytest("--codeblocks") + result.assert_outcomes(skipped=1) + + string = """ + Lorem ipsum + + + + ```python + print(1 + 3) + ``` + + + + ``` + 4 + ``` + + """ + testdir.makefile(".md", string) + result = testdir.runpytest("--codeblocks") + result.assert_outcomes(passed=1)