Skip to content

Commit d4610d7

Browse files
authored
small fixes (#4)
* stop using pytest_runtest_makereport * delete only httpdbg log files
1 parent db16985 commit d4610d7

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pip install pytest-httpdbg
1818
--httpdbg record HTTP(S) requests
1919
--httpdbg-dir=HTTPDBG_DIR
2020
save httpdbg traces in a directory
21-
--httpdbg-no-clean clean httpdbg directory
21+
--httpdbg-no-clean do not clean the httpdbg directory
2222
--httpdbg-initiator=HTTPDBG_INITIATOR
2323
add a new initiator (package) for httpdbg
2424
```
@@ -44,30 +44,39 @@ You can use any package as an initiator, this is not limited to HTTP requests.
4444

4545
## test report
4646

47-
One log file in markdown format is created per test.
47+
When the test is finished (teardown step included), one log file in markdown format is written. The path to this log file is stashed in the item when the test starts (before the setup step), even if the file not exists yet.
4848

4949
### pytest-html
5050

5151
You can copy the following code in your top-level `conftest.py` to include the logs into your pytest-html report.
5252

5353
```python
54+
import os
55+
5456
import pytest
5557

5658
from pytest_httpdbg import httpdbg_record_filename
5759

60+
5861
@pytest.hookimpl(hookwrapper=True)
5962
def pytest_runtest_makereport(item, call):
6063
pytest_html = item.config.pluginmanager.getplugin("html")
6164
outcome = yield
6265
report = outcome.get_result()
6366
extra = getattr(report, "extra", [])
6467

65-
if report.when == "teardown":
68+
if call.when == "setup":
6669
if httpdbg_record_filename in item.stash:
6770
extra.append(
6871
pytest_html.extras.url(
69-
item.stash[httpdbg_record_filename], name="HTTPDBG"
72+
os.path.basename(item.stash[httpdbg_record_filename]), name="HTTPDBG"
7073
)
7174
)
72-
report.extra = extra
73-
```
75+
report.extra = extra
76+
```
77+
78+
This example works if you use the same directory for the html test report file and the httpdbg logs.
79+
80+
`pytest demo/ --httpdbg --httpdbg-dir report --html=report/report.html`
81+
82+
If this is not the case, you must adapt it to your configuration.

pytest_httpdbg/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# -*- coding: utf-8 -*-
22
from pytest_httpdbg.plugin import httpdbg_record_filename # noqa F401
33

4-
VERSION = "0.2.0"
4+
VERSION = "0.3.0"

pytest_httpdbg/plugin.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,21 @@
11
# -*- coding: utf-8 -*-
2+
import glob
23
import os
34
import pickle
4-
import shutil
55
import time
66
from typing import Optional
77
import uuid
88

99
import pytest
1010

1111
from httpdbg import httpdbg
12-
from httpdbg import HTTPRecords
1312

14-
httpdbg_records = pytest.StashKey[HTTPRecords]()
1513
httpdbg_record_filename = pytest.StashKey[str]()
1614

1715

1816
def safe_test_name_for_filename(nodeid):
1917
safe_nodeid = "".join([c if c.isalnum() else "_" for c in nodeid])
20-
return f"{safe_nodeid}_{int(time.time()*1000)}.md"
21-
22-
23-
def pytest_runtest_makereport(item, call):
24-
httpdbg_dir = item.config.option.httpdbg_dir
25-
26-
if httpdbg_dir and len(item.stash[httpdbg_records]) > 0:
27-
os.makedirs(httpdbg_dir, exist_ok=True)
28-
29-
if httpdbg_record_filename not in item.stash:
30-
filename = os.path.join(
31-
httpdbg_dir, safe_test_name_for_filename(item.nodeid)
32-
)
33-
item.stash[httpdbg_record_filename] = filename
34-
35-
with open(item.stash[httpdbg_record_filename], "a", encoding="utf-8") as f:
36-
f.write(f"# {item.nodeid} - {call.when}\n\n")
37-
for record in item.stash[httpdbg_records]:
38-
f.write(f"{record_to_md(record)}\n")
39-
40-
item.stash[httpdbg_records].reset()
18+
return f"{safe_nodeid}_{int(time.time()*1000)}.httpdbg.md"
4119

4220

4321
def content_type_md(content_type):
@@ -94,7 +72,7 @@ def pytest_addoption(parser):
9472
"--httpdbg-no-clean",
9573
action="store_true",
9674
default=False,
97-
help="clean httpdbg directory",
75+
help="do not clean the httpdbg directory",
9876
)
9977
parser.addoption(
10078
"--httpdbg-initiator",
@@ -112,23 +90,51 @@ def pytest_configure(config):
11290
httpdbg_dir = config.option.httpdbg_dir
11391
if httpdbg_dir and not config.option.httpdbg_no_clean:
11492
if os.path.isdir(httpdbg_dir):
115-
shutil.rmtree(httpdbg_dir)
93+
for logfile in glob.glob(os.path.join(httpdbg_dir, "*.httpdbg.md")):
94+
os.remove(logfile)
95+
try:
96+
os.rmdir(httpdbg_dir)
97+
except OSError:
98+
pass # the directory is not empty, we don't remove it
11699

117100

118101
@pytest.hookimpl(hookwrapper=True)
119102
def pytest_runtest_protocol(item: pytest.Item, nextitem: Optional[pytest.Item]):
120103
if item.config.option.httpdbg or "HTTPDBG_SUBPROCESS_DIR" in os.environ:
121104
with httpdbg(initiators=item.config.option.httpdbg_initiator) as records:
122-
item.stash[httpdbg_records] = records
105+
# the record of the http requests has been enable using a pytest command line argument
106+
# -> first, we stash the path to the log file
107+
httpdbg_dir = item.config.option.httpdbg_dir
108+
if httpdbg_dir:
109+
os.makedirs(httpdbg_dir, exist_ok=True)
110+
111+
filename = os.path.join(
112+
httpdbg_dir, safe_test_name_for_filename(item.nodeid)
113+
)
114+
item.stash[httpdbg_record_filename] = filename
115+
123116
yield
124-
if "PYTEST_XDIST_WORKER" in os.environ:
125-
if "HTTPDBG_SUBPROCESS_DIR" in os.environ:
117+
118+
# pytest is executed using pyhttpdbg
119+
# -> we serialize the HTTPRecords object to share it with the main pyhttpdbg process
120+
if "HTTPDBG_SUBPROCESS_DIR" in os.environ:
121+
if "PYTEST_XDIST_WORKER" in os.environ:
126122
if len(records.requests) > 0:
127123
fname = f"{os.environ['HTTPDBG_SUBPROCESS_DIR']}/{uuid.uuid1()}"
128124
with open(f"{fname}.httpdbgrecords.tmp", "wb") as f:
129125
pickle.dump(records, f)
130126
os.rename(
131127
f"{fname}.httpdbgrecords.tmp", f"{fname}.httpdbgrecords"
132128
)
129+
130+
# the record of the http requests has been enable using a pytest command line argument
131+
# -> we create a human readable file that contains all the HTTP requests recorded
132+
if httpdbg_record_filename in item.stash:
133+
with open(
134+
item.stash[httpdbg_record_filename], "w", encoding="utf-8"
135+
) as f:
136+
f.write(f"# {item.nodeid}\n\n")
137+
for record in records:
138+
f.write(f"{record_to_md(record)}\n")
133139
else:
134140
yield

0 commit comments

Comments
 (0)