Skip to content

Commit 50dfbe7

Browse files
authored
Feature: Add 'session' to results summary hook (#660)
1 parent 48e20c1 commit 50dfbe7

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

.github/workflows/tests.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ jobs:
2626
with:
2727
python-version: '3.10'
2828

29+
- name: Ensure latest pip
30+
run: python -m pip install --upgrade pip
31+
2932
- name: Install tox
3033
run: python -m pip install --upgrade tox
3134

@@ -128,9 +131,20 @@ jobs:
128131
with:
129132
python-version: ${{ matrix.python-version }}
130133

134+
- name: Ensure latest pip
135+
run: python -m pip install --upgrade pip
136+
131137
- name: Install tox
132138
run: python -m pip install --upgrade tox
133139

140+
- name: Cache tox virtual environment
141+
uses: actions/cache@v3
142+
with:
143+
path: .tox
144+
key: ${{ matrix.os }}-tox-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml', 'tox.ini') }}
145+
restore-keys: |
146+
${{ matrix.os }}-tox-${{ matrix.python-version }}-
147+
134148
- name: Run unit tests
135149
if: ${{ ! matrix.tox-env && matrix.with-coverage }}
136150
run: tox -e py${{ matrix.python-version }}-cov -- testing/test_unit.py
@@ -196,9 +210,20 @@ jobs:
196210
with:
197211
python-version: ${{ matrix.python-version }}
198212

213+
- name: Ensure latest pip
214+
run: python -m pip install --upgrade pip
215+
199216
- name: Install tox
200217
run: python -m pip install --upgrade tox
201218

219+
- name: Cache tox virtual environment
220+
uses: actions/cache@v3
221+
with:
222+
path: .tox
223+
key: ubuntu-latest-tox-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml', 'tox.ini') }}
224+
restore-keys: |
225+
ubuntu-latest-tox-${{ matrix.python-version }}-
226+
202227
- name: Run integration tests
203228
if: ${{ ! matrix.tox-env && matrix.with-coverage }}
204229
run: tox -e ${{ matrix.python-version }}-cov -- testing/test_integration.py

src/pytest_html/basereport.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def pytest_sessionfinish(self, session):
173173
prefix=self._report.data["additionalSummary"]["prefix"],
174174
summary=self._report.data["additionalSummary"]["summary"],
175175
postfix=self._report.data["additionalSummary"]["postfix"],
176+
session=session,
176177
)
177178
self._report.set_data("runningState", "Finished")
178179
self._generate_report()

src/pytest_html/hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def pytest_html_report_title(report):
77
"""Called before adding the title to the report"""
88

99

10-
def pytest_html_results_summary(prefix, summary, postfix):
10+
def pytest_html_results_summary(prefix, summary, postfix, session):
1111
"""Called before adding the summary section to the report"""
1212

1313

testing/test_integration.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from base64 import b64encode
99
from pathlib import Path
1010

11-
import pkg_resources
1211
import pytest
1312
from assertpy import assert_that
1413
from bs4 import BeautifulSoup
@@ -28,12 +27,8 @@
2827

2928

3029
def run(pytester, path="report.html", cmd_flags=None, query_params=None):
31-
if cmd_flags is None:
32-
cmd_flags = []
33-
34-
if query_params is None:
35-
query_params = {}
36-
query_params = urllib.parse.urlencode(query_params)
30+
cmd_flags = cmd_flags or []
31+
query_params = urllib.parse.urlencode(query_params) if query_params else {}
3732

3833
path = pytester.path.joinpath(path)
3934
pytester.runpytest("--html", path, *cmd_flags)
@@ -120,6 +115,8 @@ def file_content():
120115
)
121116
except AttributeError:
122117
# Needed for python < 3.9
118+
import pkg_resources
119+
123120
return pkg_resources.resource_string(
124121
"pytest_html", os.path.join("resources", "style.css")
125122
).decode("utf-8")

testing/test_unit.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
pytest_plugins = ("pytester",)
22

33

4-
def run(pytester, path="report.html", *args):
4+
def run(pytester, path="report.html", cmd_flags=None):
5+
cmd_flags = cmd_flags or []
56
path = pytester.path.joinpath(path)
6-
return pytester.runpytest("--html", path, *args)
7+
return pytester.runpytest("--html", path, *cmd_flags)
78

89

910
def test_duration_format_deprecation_warning(pytester):
@@ -19,8 +20,27 @@ def pytest_runtest_makereport(item, call):
1920
)
2021
pytester.makepyfile("def test_pass(): pass")
2122
result = run(pytester)
23+
result.assert_outcomes(passed=1)
2224
result.stdout.fnmatch_lines(
2325
[
2426
"*DeprecationWarning: 'duration_formatter'*",
2527
],
2628
)
29+
30+
31+
def test_html_results_summary_hook(pytester):
32+
pytester.makeconftest(
33+
"""
34+
import pytest
35+
36+
def pytest_html_results_summary(prefix, summary, postfix, session):
37+
print(prefix)
38+
print(summary)
39+
print(postfix)
40+
print(session)
41+
"""
42+
)
43+
44+
pytester.makepyfile("def test_pass(): pass")
45+
result = run(pytester)
46+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)