3

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.

Roochiedoor
  • 887
  • 12
  • 19
  • 2
    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`](https://github.com/pytest-dev/pytest-html/blob/master/pytest_html/resources/main.js#L84). The only way I see is to replace the `main.js` file with the one without the `if (collapsed.includes(elem.innerHTML))` check. – hoefling Jul 26 '18 at 10:33
  • 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 Jul 31 '18 at 12:28

2 Answers2

4

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.

john
  • 56
  • 2
  • 1
    Thanks @john the pytest.ini render_collapsed option is good to know. The document you provided a link to, still doesn't mention which file one actually puts the ?collapsed parameter in, which was the basis of my confusion when I asked the question. – Roochiedoor May 20 '21 at 23:09
  • 1
    @Roochiedoor Add it in pytest.ini file, or any pytest config files. [pytest] render_collapsed = True – yasbars Jun 15 '23 at 08:53
1

Just to add to @john answer and the other steps I have tried:

For some reason,

[pytest]
render_collapsed = failed,error

Does not work for me, based on the steps here: https://docs.pytest.org/en/latest/reference/customize.html#configuration-file-formats

When using the above configuration, I get the following error: KeyError: 'render_collapsed' ValueError: invalid truth value 'failed,error'

To resolve the issue: Create a Configuration file on the directory where you will run the command. In my case I use pytest.ini

[pytest]
render_collapsed = True

Then run: pytest --html=report.html --self-contained-html

report.html is the name of the report file

--self-contained-html creates the report with inline css. This will allows you to simply share the report.html file instead of sharing a folder containing html and css files

More information about other Configuration file formats(pytest.ini, pyproject.toml, tox.ini, setup.cfg) can be found here: https://docs.pytest.org/en/latest/reference/customize.html#configuration-file-formats

yasbars
  • 171
  • 3
  • 7