forked from pytest-dev/pytest-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_unit.py
148 lines (119 loc) · 3.6 KB
/
test_unit.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
import importlib.resources
import sys
from pathlib import Path
import pytest
from assertpy import assert_that
pytest_plugins = ("pytester",)
def run(pytester, path="report.html", cmd_flags=None):
cmd_flags = cmd_flags or []
path = pytester.path.joinpath(path)
return pytester.runpytest("--html", path, *cmd_flags)
def file_content():
return (
importlib.resources.files("pytest_html")
.joinpath("assets", "style.css")
.read_bytes()
.decode("utf-8")
.strip()
)
def test_duration_format_deprecation_warning(pytester):
pytester.makeconftest(
"""
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
setattr(report, "duration_formatter", "%H:%M:%S.%f")
"""
)
pytester.makepyfile("def test_pass(): pass")
result = run(pytester)
result.assert_outcomes(passed=1)
result.stdout.fnmatch_lines(
[
"*DeprecationWarning: 'duration_formatter'*",
],
)
def test_html_results_summary_hook(pytester):
pytester.makeconftest(
"""
import pytest
def pytest_html_results_summary(prefix, summary, postfix, session):
print(prefix)
print(summary)
print(postfix)
print(session)
"""
)
pytester.makepyfile("def test_pass(): pass")
result = run(pytester)
result.assert_outcomes(passed=1)
def test_chdir(pytester):
pytester.makepyfile(
"""
import pytest
@pytest.fixture
def changing_dir(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
def test_function(changing_dir):
pass
"""
)
report_path = Path("reports") / "report.html"
page = pytester.runpytest("--html", str(report_path))
assert page.ret == 0
assert (
f"Generated html report: {(pytester.path / report_path).as_uri()}"
) in page.outlines[-2]
@pytest.fixture
def css_file_path(pytester):
css_one = """
h1 {
color: red;
}
"""
css_two = """
h2 {
color: blue;
}
"""
css_dir = pytester.path / "extra_css"
css_dir.mkdir()
file_path = css_dir / "one.css"
with open(file_path, "w") as f:
f.write(css_one)
pytester.makefile(".css", two=css_two)
pytester.makepyfile("def test_pass(): pass")
return file_path
@pytest.fixture(params=[True, False])
def expandvar(request, css_file_path, monkeypatch):
if request.param:
monkeypatch.setenv("EXTRA_CSS", str(css_file_path))
return "%EXTRA_CSS%" if sys.platform == "win32" else "${EXTRA_CSS}"
return css_file_path
def test_custom_css(pytester, css_file_path, expandvar):
result = run(
pytester, "report.html", cmd_flags=["--css", expandvar, "--css", "two.css"]
)
result.assert_outcomes(passed=1)
path = pytester.path.joinpath("assets", "style.css")
with open(str(path)) as f:
css = f.read()
assert_that(css).contains("* " + str(css_file_path)).contains("* two.css")
def test_custom_css_selfcontained(pytester, css_file_path, expandvar):
result = run(
pytester,
"report.html",
cmd_flags=[
"--css",
expandvar,
"--css",
"two.css",
"--self-contained-html",
],
)
result.assert_outcomes(passed=1)
with open(pytester.path / "report.html") as f:
html = f.read()
assert_that(html).contains("* " + str(css_file_path)).contains("* two.css")