Skip to content

Commit f41baf9

Browse files
authored
add httpdbg html report export in allure report (#19)
1 parent 26c4368 commit f41baf9

File tree

8 files changed

+33
-450
lines changed

8 files changed

+33
-450
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ All you need to do is add the `--httpdbg-allure` option to your pytest command l
1818
pytest ../httpdbg-docs/examples/ --alluredir=./allure-results --httpdbg-allure
1919
```
2020

21-
If an HTTP request is made by the test (or within a fixture, during the setup or teardown phase), the request will be saved in the Allure report under a step called `httpdbg`.
21+
If an HTTP request is made by the test (or within a fixture, during the setup or teardown phase), a HTTP traces report will be saved in the Allure report under a step called `httpdbg`.
2222

23-
![](https://github.com/cle-b/pytest-httpdbg/blob/main/pytest-httpdbg-allure-0.8.0.png?raw=true)
23+
### compact mode
24+
![](https://github.com/cle-b/pytest-httpdbg/blob/main/pytest-httpdbg-allure-compact-0.10.0.png?raw=true)
25+
26+
### full mode
27+
![](https://github.com/cle-b/pytest-httpdbg/blob/main/pytest-httpdbg-allure-full-0.10.0.png?raw=true)
2428

2529

2630
## Custom test report
@@ -77,8 +81,6 @@ reporting:
7781
--httpdbg-no-clean do not clean the httpdbg directory
7882
7983
--httpdbg-allure save HTTP(S) traces into the allure report
80-
--httpdbg-no-headers do not save the HTTP headers
81-
--httpdbg-no-binary do not save the HTTP payload if it's a binary content
8284
--httpdbg-only-on-failure save the HTTP requests only if the test failed
8385
8486
--httpdbg-initiator=HTTPDBG_INITIATOR add a new initiator (package) for httpdbg

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ classifiers = [
2424
]
2525
dynamic = ["version"]
2626
dependencies = [
27-
"httpdbg>=1.2.5",
27+
"httpdbg>=2.1.1",
2828
"pytest>=7.0.0"
2929
]
3030

pytest-httpdbg-allure-0.8.0.png

-108 KB
Binary file not shown.
108 KB
Loading
125 KB
Loading

pytest_httpdbg/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from pytest_httpdbg.plugin import httpdbg_record_filename # noqa F401
22

3-
__version__ = "0.9.1"
3+
__version__ = "0.10.0"

pytest_httpdbg/plugin.py

Lines changed: 8 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import glob
22
import os
33
import time
4-
import traceback
54
from typing import Optional
65

76
import pytest
87

98
from httpdbg import httprecord
9+
from httpdbg.export import generate_html
1010

1111
httpdbg_record_filename = pytest.StashKey[str]()
1212

@@ -88,20 +88,6 @@ def pytest_addoption(parser):
8888
help="save HTTP(S) traces into the allure report",
8989
)
9090

91-
reporting_group.addoption(
92-
"--httpdbg-no-headers",
93-
action="store_true",
94-
default=False,
95-
help="do not save the HTTP headers",
96-
)
97-
98-
reporting_group.addoption(
99-
"--httpdbg-no-binary",
100-
action="store_true",
101-
default=False,
102-
help="do not save the HTTP payload if it's a binary content",
103-
)
104-
10591
reporting_group.addoption(
10692
"--httpdbg-only-on-failure",
10793
action="store_true",
@@ -183,52 +169,6 @@ def pytest_sessionfinish(session, exitstatus):
183169
session.httpdbg_recorder.__exit__(None, None, None)
184170

185171

186-
def get_allure_attachment_type_from_content_type(content_type: str):
187-
try:
188-
import allure
189-
190-
content_type = content_type.split(";", 1)[0].strip()
191-
192-
for attachment_type in allure.attachment_type:
193-
if attachment_type.mime_type.lower() == content_type.lower():
194-
return attachment_type
195-
except ImportError:
196-
pass
197-
return None
198-
199-
200-
def req_resp_steps(label, req, save_headers, save_binary_payload):
201-
try:
202-
import allure
203-
204-
# we generate the payload first because we do not want to add a step
205-
# if there is no headers and no payload to save
206-
content = req.preview
207-
payload = None
208-
if content.get("text"):
209-
payload = content.get("text")
210-
elif save_binary_payload:
211-
payload = req.content
212-
213-
if save_headers or payload:
214-
with allure.step(label):
215-
if save_headers:
216-
allure.attach(
217-
req.rawheaders.decode("utf-8"),
218-
name="headers",
219-
attachment_type=allure.attachment_type.TEXT,
220-
)
221-
if payload:
222-
attachment_type = get_allure_attachment_type_from_content_type(
223-
content.get("content_type", "")
224-
)
225-
allure.attach(
226-
payload, name="payload", attachment_type=attachment_type
227-
)
228-
except ImportError:
229-
pass
230-
231-
232172
@pytest.hookimpl(hookwrapper=True)
233173
def pytest_runtest_makereport(item, call):
234174

@@ -246,73 +186,13 @@ def pytest_runtest_makereport(item, call):
246186

247187
with allure.step("httpdbg"):
248188

249-
records = item.session.httpdbg_records
250-
251-
for record in records:
252-
253-
label = ""
254-
255-
if record.response.status_code:
256-
label += f"{record.response.status_code} "
257-
258-
if record.request.method:
259-
label += f"{record.request.method} "
260-
261-
if record.request.uri:
262-
url = record.request.uri
263-
else:
264-
url = record.url
265-
if len(url) > 200:
266-
url = url[:100] + "..." + url[-97:]
267-
ex = (
268-
(str(type(record.exception)) + " ")
269-
if record.exception is not None
270-
else ""
271-
)
272-
label += f"{ex}{url}"
273-
274-
if record.tag:
275-
label += f" (from {record.tag})"
276-
277-
with allure.step(label):
278-
details = record.url
279-
details += f"\n\nstatus: {record.response.status_code} {record.response.message}"
280-
details += f"\n\nstart: {record.tbegin.isoformat()}"
281-
details += f"\nend: {record.last_update.isoformat()}"
282-
283-
if record.initiator_id in records.initiators:
284-
details += f"\n\n{records.initiators[record.initiator_id].short_stack}"
285-
286-
if record.exception is not None:
287-
details += (
288-
f"\n\nException: {type(record.exception)}\n"
289-
)
290-
details += "".join(
291-
traceback.format_exception(
292-
type(record.exception),
293-
record.exception,
294-
record.exception.__traceback__,
295-
)
296-
)
297-
298-
allure.attach(
299-
details,
300-
name="details",
301-
attachment_type=allure.attachment_type.TEXT,
302-
)
303-
304-
req_resp_steps(
305-
"request",
306-
record.request,
307-
not item.config.option.httpdbg_no_headers,
308-
not item.config.option.httpdbg_no_binary,
309-
)
310-
req_resp_steps(
311-
"response",
312-
record.response,
313-
not item.config.option.httpdbg_no_headers,
314-
not item.config.option.httpdbg_no_binary,
315-
)
189+
allure.attach(
190+
generate_html(
191+
item.session.httpdbg_records, for_export=True
192+
),
193+
name="http traces",
194+
attachment_type=allure.attachment_type.HTML,
195+
)
316196
except ImportError:
317197
pass
318198

0 commit comments

Comments
 (0)