1
votes

I use the pytest runner to get the output of results from my automated test frameworks (Selenium and RestAPI tests). I use the pytest-html plugin to generate a single page html result file at the end of the test. I initiate the test run with the following command (from an active VirtEnv session).

python -m pytest -v --html="api_name_test.html" --self-contained-html

(it's a little more complicated in that I use a powershell script to run this and provide a datetime stamped result file name and email the file when it's finished but it's essentially the above command)

When the reports are generated and I open this report html I find that all the non passing tests are expanded. I want to make it so all rows are collapsed by default (Failed, XFailed, Error etc..).

html result with some files expanded

My project contains a conftest.py file at the diretory root and a pytest.ini file where I specify the directory for the test scripts

In the conftest.py file in my simplest projects, I have one optional hook to obtain the target url of the tests and put that in the report summary:

import pytest
from py._xmlgen import html
import os
import rootdir_ref
import simplejson

@pytest.mark.optionalhook
def pytest_html_results_summary(prefix):
    theRootDir = os.path.dirname(rootdir_ref.__file__)
    credentials_path = os.path.join(theRootDir, 'TestDataFiles', 'API_Credentials.txt')
    target_url = simplejson.load(open(credentials_path)).get('base_url')
    prefix.extend([html.p("Testing against URL: " + target_url)])

The Github page mentions that a display query can be used to collapse rows with various results, but it doesn't mention where this information is entered. https://github.com/pytest-dev/pytest-html

"By default, all rows in the Results table will be expanded except those that have Passed. This behavior can be customized with a query parameter: ?collapsed=Passed,XFailed,Skipped"

Currently I'm unsure if the ?collapsed=... line goes in the command line, or the conftest as a hook, or do I need to edit the default style.css or main.js that comes with the pytest-html plugin? (Also i'm not familiar with css and only know a small amount of html). I'm assuming it goes in the conftest.py file as a hook but don't really understand how to apply it.

1
The query parameter is passed to the generated HTML report, so e.g. when you open it in browser, use the URL file:///path/to/report.html?collapsed=Passed,XFailed,Skipped. This is something you can't adapt from the hooks because the collapsing of test blocks is handled entirely in main.js. The only way I see is to replace the main.js file with the one without the if (collapsed.includes(elem.innerHTML)) check.hoefling
Awesome, thanks @hoefling , I couldn’t find anywhere that indicated how to use that option. I’ll try experimenting with the JS file and see if I can make it do what I want.Roochiedoor

1 Answers

1
votes

https://pytest-html.readthedocs.io/en/latest/user_guide.html#display-options

Auto Collapsing Table Rows

By default, all rows in the Results table will be expanded except those that have Passed.

This behavior can be customized either with a query parameter: ?collapsed=Passed,XFailed,Skipped or by setting the render_collapsed in a configuration file (pytest.ini, setup.cfg, etc).

[pytest] render_collapsed = True

NOTE: Setting render_collapsed will, unlike the query parameter, affect all statuses.