Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ All you need to do is add the `--httpdbg-allure` option to your pytest command l
pytest ../httpdbg-docs/examples/ --alluredir=./allure-results --httpdbg-allure
```

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`.
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`.

![](https://github.com/cle-b/pytest-httpdbg/blob/main/pytest-httpdbg-allure-0.8.0.png?raw=true)
### compact mode
![](https://github.com/cle-b/pytest-httpdbg/blob/main/pytest-httpdbg-allure-compact-0.10.0.png?raw=true)

### full mode
![](https://github.com/cle-b/pytest-httpdbg/blob/main/pytest-httpdbg-allure-full-0.10.0.png?raw=true)


## Custom test report
Expand Down Expand Up @@ -77,8 +81,6 @@ reporting:
--httpdbg-no-clean do not clean the httpdbg directory

--httpdbg-allure save HTTP(S) traces into the allure report
--httpdbg-no-headers do not save the HTTP headers
--httpdbg-no-binary do not save the HTTP payload if it's a binary content
--httpdbg-only-on-failure save the HTTP requests only if the test failed

--httpdbg-initiator=HTTPDBG_INITIATOR add a new initiator (package) for httpdbg
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ classifiers = [
]
dynamic = ["version"]
dependencies = [
"httpdbg>=1.2.5",
"httpdbg>=2.1.1",
"pytest>=7.0.0"
]

Expand Down
Binary file removed pytest-httpdbg-allure-0.8.0.png
Binary file not shown.
Binary file added pytest-httpdbg-allure-compact-0.10.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pytest-httpdbg-allure-full-0.10.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pytest_httpdbg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pytest_httpdbg.plugin import httpdbg_record_filename # noqa F401

__version__ = "0.9.1"
__version__ = "0.10.0"
136 changes: 8 additions & 128 deletions pytest_httpdbg/plugin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import glob
import os
import time
import traceback
from typing import Optional

import pytest

from httpdbg import httprecord
from httpdbg.export import generate_html

httpdbg_record_filename = pytest.StashKey[str]()

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

reporting_group.addoption(
"--httpdbg-no-headers",
action="store_true",
default=False,
help="do not save the HTTP headers",
)

reporting_group.addoption(
"--httpdbg-no-binary",
action="store_true",
default=False,
help="do not save the HTTP payload if it's a binary content",
)

reporting_group.addoption(
"--httpdbg-only-on-failure",
action="store_true",
Expand Down Expand Up @@ -183,52 +169,6 @@ def pytest_sessionfinish(session, exitstatus):
session.httpdbg_recorder.__exit__(None, None, None)


def get_allure_attachment_type_from_content_type(content_type: str):
try:
import allure

content_type = content_type.split(";", 1)[0].strip()

for attachment_type in allure.attachment_type:
if attachment_type.mime_type.lower() == content_type.lower():
return attachment_type
except ImportError:
pass
return None


def req_resp_steps(label, req, save_headers, save_binary_payload):
try:
import allure

# we generate the payload first because we do not want to add a step
# if there is no headers and no payload to save
content = req.preview
payload = None
if content.get("text"):
payload = content.get("text")
elif save_binary_payload:
payload = req.content

if save_headers or payload:
with allure.step(label):
if save_headers:
allure.attach(
req.rawheaders.decode("utf-8"),
name="headers",
attachment_type=allure.attachment_type.TEXT,
)
if payload:
attachment_type = get_allure_attachment_type_from_content_type(
content.get("content_type", "")
)
allure.attach(
payload, name="payload", attachment_type=attachment_type
)
except ImportError:
pass


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):

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

with allure.step("httpdbg"):

records = item.session.httpdbg_records

for record in records:

label = ""

if record.response.status_code:
label += f"{record.response.status_code} "

if record.request.method:
label += f"{record.request.method} "

if record.request.uri:
url = record.request.uri
else:
url = record.url
if len(url) > 200:
url = url[:100] + "..." + url[-97:]
ex = (
(str(type(record.exception)) + " ")
if record.exception is not None
else ""
)
label += f"{ex}{url}"

if record.tag:
label += f" (from {record.tag})"

with allure.step(label):
details = record.url
details += f"\n\nstatus: {record.response.status_code} {record.response.message}"
details += f"\n\nstart: {record.tbegin.isoformat()}"
details += f"\nend: {record.last_update.isoformat()}"

if record.initiator_id in records.initiators:
details += f"\n\n{records.initiators[record.initiator_id].short_stack}"

if record.exception is not None:
details += (
f"\n\nException: {type(record.exception)}\n"
)
details += "".join(
traceback.format_exception(
type(record.exception),
record.exception,
record.exception.__traceback__,
)
)

allure.attach(
details,
name="details",
attachment_type=allure.attachment_type.TEXT,
)

req_resp_steps(
"request",
record.request,
not item.config.option.httpdbg_no_headers,
not item.config.option.httpdbg_no_binary,
)
req_resp_steps(
"response",
record.response,
not item.config.option.httpdbg_no_headers,
not item.config.option.httpdbg_no_binary,
)
allure.attach(
generate_html(
item.session.httpdbg_records, for_export=True
),
name="http traces",
attachment_type=allure.attachment_type.HTML,
)
except ImportError:
pass

Expand Down
Loading