Skip to content

Commit ca07d1f

Browse files
committed
Start to create a real documentation
1 parent 3f622f3 commit ca07d1f

8 files changed

+86
-36
lines changed

docs/test_demo_with_approvals.approved.adoc

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
= Demo with approvals
22
This file is an example of using doc as test with pytest.
33

4-
include::test_demo_with_approvals.test_first_chapter.approved.adoc[leveloffset=+1]
5-
include::test_demo_with_approvals.test_second_chapter.approved.adoc[leveloffset=+1]
6-
include::test_demo_with_approvals.test_third_chapter.approved.adoc[leveloffset=+1]
4+
include::test_demo_with_approvals.test_simple_chapter.approved.adoc[leveloffset=+1]
5+
include::test_demo_with_approvals.test_using_doc_string.approved.adoc[leveloffset=+1]
76
include::test_demo_with_approvals.test_chapter_with_parameters_Arg_A_.approved.adoc[leveloffset=+1]
87
include::test_demo_with_approvals.test_chapter_with_parameters_Arg_B_.approved.adoc[leveloffset=+1]
98
include::test_demo_with_approvals.TestClassDemo.approved.adoc[leveloffset=+1]

docs/test_demo_with_approvals.test_first_chapter.approved.adoc

-5
This file was deleted.

docs/test_demo_with_approvals.test_second_chapter.approved.adoc

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
= Simple chapter
2+
3+
4+
You can write in your test the information you want to see in your documentation.
5+
6+
.Code use to generate this chapter
7+
[%collapsible]
8+
====
9+
[source,python,indent=0]
10+
----
11+
12+
def test_simple_chapter(request, doc):
13+
doc.write(textwrap.dedent("""
14+
You can write in your test the information you want to see in your documentation.
15+
"""))
16+
17+
18+
----
19+
====

docs/test_demo_with_approvals.test_third_chapter.approved.adoc

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
= Using doc string
2+
3+
The description of the chapter can be done using Python docstring.
4+
5+
6+
This doc string is placed in the document just under the title and before the text added within the test function.
7+
8+
.Code use to generate this chapter
9+
[%collapsible]
10+
====
11+
[source,python,indent=0]
12+
----
13+
14+
def test_using_doc_string(request, doc):
15+
"""
16+
The description of the chapter can be done using Python docstring.
17+
"""
18+
19+
doc.write(textwrap.dedent("""
20+
This doc string is placed in the document just under the title and before the text added within the test function.
21+
"""))
22+
23+
24+
----
25+
====

src/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ def pytest_runtest_makereport(item, call):
1414
if doc_fixture_name in item.funcargs:
1515
doc = item.funcargs[doc_fixture_name]
1616
if isinstance(doc, DocAsTest):
17-
doc.write(f"Exception executing test:\n----\n{rep.longreprtext}\n----\n")
17+
doc.write(f"\n********************\nException executing test:\n----\n{rep.longreprtext}\n----\n")

src/test_demo_with_approvals.py

+39-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
11
"""
22
This file is an example of using doc as test with pytest.
33
"""
4+
import inspect
45
import os
56
import pytest
7+
import textwrap
68
from doc_as_test_pytest import DocAsTest, doc, doc_module
79

8-
9-
10-
def test_first_chapter(doc):
10+
class CodeExtractor:
11+
def extract_code(object, tag:str):
12+
return inspect.getsource(object)\
13+
.split(f"# >>>{tag}")[1]\
14+
.split(f"# <<<{tag}")[0]
15+
16+
def format_code(request, tag):
17+
code = CodeExtractor.extract_code(request.module, tag)
18+
return (textwrap.dedent("""\
19+
20+
.Code use to generate this chapter
21+
[%collapsible]
22+
====
23+
[source,python,indent=0]
24+
----
25+
{}
26+
----
27+
====
28+
""").format(code))
29+
30+
# >>>simple_chapter
31+
def test_simple_chapter(request, doc):
32+
doc.write(textwrap.dedent("""
33+
You can write in your test the information you want to see in your documentation.
34+
"""))
35+
36+
# <<<simple_chapter
37+
doc.write(format_code(request, "simple_chapter"))
38+
39+
# >>>using_doc_string
40+
def test_using_doc_string(request, doc):
1141
"""
12-
The description of the chapter can be done with docstring.
42+
The description of the chapter can be done using Python docstring.
1343
"""
1444

15-
doc.write("You can write in your test the information you want to see in your documentation.")
16-
17-
18-
def test_second_chapter(doc):
19-
doc.write("""
20-
The text written in the doc should provide enough information to say if the behaviour is correct.
45+
doc.write(textwrap.dedent("""
46+
This doc string is placed in the document just under the title and before the text added within the test function.
47+
"""))
2148

22-
Generally, we found input and output.
23-
""")
24-
25-
26-
def test_third_chapter(doc):
27-
28-
doc.write("Nothing new for this third chapter")
49+
# <<<using_doc_string
50+
doc.write(format_code(request, "using_doc_string"))
2951

3052
@pytest.mark.parametrize("param", [
3153
("Arg A"),

0 commit comments

Comments
 (0)