-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_pytest_reporter.py
152 lines (120 loc) · 4.69 KB
/
test_pytest_reporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pytest
import _pytest
pytest_plugins = ["pytester"]
@pytest.fixture
def template(testdir):
testdir.makeconftest(
"""
import json
import pytest
def pytest_reporter_render(template_name, dirs, context):
return template_name
"""
)
@pytest.fixture
def get_context(testdir, template):
def _get_context():
hooks = testdir.inline_run("--template=dummy.txt", "--report=report.txt")
return hooks.getcall("pytest_reporter_render").context
return _get_context
def test_file_is_saved(testdir, template):
testdir.makepyfile("def test_pass(): pass")
testdir.runpytest("--template=dummy.txt", "--report=report.txt")
report = testdir.tmpdir.join("report.txt")
assert report.read() == "dummy.txt"
def test_top_level_context(testdir, get_context):
testdir.makepyfile("def test_pass(): pass")
context = get_context()
assert isinstance(context["config"], _pytest.config.Config)
assert isinstance(context["session"], _pytest.main.Session)
assert isinstance(context["started"], float)
assert isinstance(context["ended"], float)
def test_tests(testdir, get_context):
testdir.makepyfile("def test_pass(): pass")
context = get_context()
assert "tests" in context
assert len(context["tests"]) == 1
test = context["tests"][0]
assert "item" in test
assert isinstance(test["item"], _pytest.nodes.Item)
assert test["item"].name == "test_pass"
assert "phases" in test
assert len(test["phases"]) == 3
for phase, when in zip(test["phases"], ["setup", "call", "teardown"]):
assert "call" in phase
assert "report" in phase
assert "log_records" in phase
assert "status" in phase
assert phase["call"].when == when
assert phase["report"].when == when
assert phase["report"].outcome == "passed"
def test_xdist_loadgroup(testdir):
testdir.makepyfile(
"""
import pytest
@pytest.mark.xdist_group("group1")
def test_pass(): pass
""")
hooks = testdir.inline_run(
"--template=dummy.txt", "--report=report.txt", "-n=1", "--dist=loadgroup"
)
context = hooks.getcall("pytest_reporter_render").context
assert "tests" in context
assert len(context["tests"]) == 1
test = context["tests"][0]
assert "item" in test
assert isinstance(test["item"], _pytest.nodes.Item)
assert test["item"].name == "test_pass"
assert "phases" in test
assert len(test["phases"]) == 3
for phase, when in zip(test["phases"], ["setup", "call", "teardown"]):
assert "report" in phase
assert "log_records" in phase
assert "status" in phase
assert phase["report"].when == when
assert phase["report"].outcome == "passed"
def test_status(testdir, get_context):
testdir.makepyfile(
"""
def test_pass(): assert True
def test_fail(): assert False
"""
)
context = get_context()
passed, failed = context["tests"]
assert passed["status"]["category"] == "passed"
assert passed["status"]["letter"] == "."
assert passed["status"]["word"] == "PASSED"
assert isinstance(passed["status"]["style"], dict)
assert failed["status"]["category"] == "failed"
assert failed["status"]["letter"] == "F"
assert failed["status"]["word"] == "FAILED"
assert isinstance(failed["status"]["style"], dict)
# setup
assert passed["phases"][0]["status"]["category"] == ""
assert passed["phases"][0]["status"]["letter"] == ""
assert passed["phases"][0]["status"]["word"] == ""
assert passed["phases"][0]["status"]["style"] == {}
# call
assert passed["phases"][1]["status"]["category"] == "passed"
assert passed["phases"][1]["status"]["letter"] == "."
assert passed["phases"][1]["status"]["word"] == "PASSED"
# teardown
assert passed["phases"][2]["status"]["category"] == ""
assert passed["phases"][2]["status"]["letter"] == ""
assert passed["phases"][2]["status"]["word"] == ""
assert passed["phases"][2]["status"]["style"] == {}
# setup
assert failed["phases"][0]["status"]["category"] == ""
assert failed["phases"][0]["status"]["letter"] == ""
assert failed["phases"][0]["status"]["word"] == ""
assert failed["phases"][0]["status"]["style"] == {}
# call
assert failed["phases"][1]["status"]["category"] == "failed"
assert failed["phases"][1]["status"]["letter"] == "F"
assert failed["phases"][1]["status"]["word"] == "FAILED"
# teardown
assert failed["phases"][2]["status"]["category"] == ""
assert failed["phases"][2]["status"]["letter"] == ""
assert failed["phases"][2]["status"]["word"] == ""
assert failed["phases"][2]["status"]["style"] == {}