pytest-dev / pytest-dev/pytest-html
Plotly graphs not rendered in extras data
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 779
- Forks
- 260
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying to add Plotly graphs in my test report, but they are somehow not visible when I open the report (either Chrome or Edge).
To reproduce, two files need to be created: conftest.py and test_dummy_plot.py. Bellow, the content of those two files, respectively:
import pytest
import pytest_html
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if call.when == 'call':
extras = getattr(report, 'extras', [])
fig = item.user_properties[0]
c = fig.to_html(full_html=False, div_id='my_plot', include_plotlyjs='cdn')
fig.write_html('my_plot.html', include_plotlyjs='cdn') # export the figure to HTML to validate its content (just for debugging).
extras.append(pytest_html.extras.html("<div>Additional HTML 1</div>")) # only for debugging.
extras.append(pytest_html.extras.html(c))
extras.append(pytest_html.extras.html("<div>Additional HTML 2</div>")) # only for debugging.
report.extras = extras
import plotly.graph_objects as go
import pytest
from plotly.subplots import make_subplots
def test_example(request: pytest.FixtureRequest):
fig = make_subplots()
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 4, 8]))
request.node.user_properties.append(fig)
Now, when running the test by invoking python -m pytest --html=report.html --self-contained-html . in the folder containing those two files, two new files are generated:
- my_plot.html
- report.html
Here is a screenshot of the plot itself, when opening my_plot.html:
And now, a screenshot of the test report, when opening report.html:
We can see that the plot is not rendered, even if present in the HTML file, as depicted in the following screenshot:
I'm not really sure why it is not rendered properly. I raised a question on StackOverflow and it seems that a solution exists, but I don't know how to integrate it properly.
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 provided conftest.py and test_dummy_plot.py, then run python -m pytest --html=report.html --self-contained-html . to compare report.html with the standalone my_plot.html. Trace how the Plotly HTML is embedded in report.html and use the linked StackOverflow solution as context. Done means the Plotly graph renders in the generated pytest report in Chrome and Edge.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- plotly, python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100