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.