pytest-dev / pytest-dev/pytest-html
Unable to save additional screenshots
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 779
- Forks
- 260
- PR merge metrics
- No merged PRs in 30d
Description
Sure, is not related to this at all.
I have the following code to display the screenshots I'm taking during my tests in the report:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item):
"""
This method takes the images in the folder of the report and writes them inside the report.
"""
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
# Get the images to be displayed inside the report
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
node_id = item.funcargs['request'].node.nodeid
# Get output directory
directory = _get_report_folder()
# Extract images for current tc
for file in os.listdir(directory):
if re.search(re.escape(f'{node_id}_'), file):
image = os.path.join(directory, file)
with open(image, "rb") as image_file:
screenshot = base64.b64encode(image_file.read())
extra.append(pytest_html.extras.image(screenshot.decode(), ''))
# Remove as it is already saved
try:
os.remove(image)
except OSError:
pass
report.extra = extra
In the new version it throws:
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\_pytest\main.py", line 270, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\_pytest\main.py", line 324, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\_pytest\main.py", line 349, in pytest_runtestloop
INTERNALERROR> item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\_pytest\runner.py", line 112, in pytest_runtest_protocol
INTERNALERROR> runtestprotocol(item, nextitem=nextitem)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\_pytest\runner.py", line 131, in runtestprotocol
INTERNALERROR> reports.append(call_and_report(item, "call", log))
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\_pytest\runner.py", line 222, in call_and_report
INTERNALERROR> report: TestReport = hook.pytest_runtest_makereport(item=item, call=call)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "<my_path>\.venv\lib\site-packages\pluggy\_callers.py", line 55, in _multicall
INTERNALERROR> gen.send(outcome)
INTERNALERROR> File "<my_path>\conftest.py", line 76, in pytest_runtest_makereport
INTERNALERROR> extra.append(pytest_html.extras.image(screenshot.decode(), ''))
INTERNALERROR> AttributeError: 'function' object has no attribute 'image'
Originally posted by @nck974 in https://github.com/pytest-dev/pytest-html/issues/581#issuecomment-1464632768
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the conftest.py pytest_runtest_makereport hook at line 76 and the failing pytest_html.extras.image call shown in the traceback. Reproduce the additional-screenshot workflow and inspect the plugin API used by that hook; done means screenshots are added to the report without the AttributeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100