Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to change the duration format from just seconds to something like hh:mm:ss? #376

Closed
brettnolan opened this issue Nov 20, 2020 · 8 comments · Fixed by #380
Closed
Assignees
Labels
enhancement This issue/PR relates to a feature request. question This PR is asking whether it is possible to achieve a certain outcome

Comments

@brettnolan
Copy link

Is there a way to format the duration time to something like:
HH:MM:SS
or
h hours, m mins, s secs

I tried doing it within here, by changing the attribute value

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

    # change duration format here
    duration = getattr(report, "duration", 0.0)                          
    converted_duration = timedelta(seconds=duration)
    setattr(report, "duration", converted_duration)

However, plugin.py didn't like the format

INTERNALERROR>   File python-virtual-environments/python3_env/lib/python3.6/site-packages/pytest_html/plugin.py", line 162, in __init__
INTERNALERROR>     html.td(f"{self.time:.2f}", class_="col-duration"),
INTERNALERROR> TypeError: unsupported format string passed to datetime.timedelta.__format__
@gnikonorov gnikonorov self-assigned this Nov 22, 2020
@gnikonorov gnikonorov added the question This PR is asking whether it is possible to achieve a certain outcome label Nov 22, 2020
@gnikonorov
Copy link
Member

Hi @brettnolan let me look into this for you

@gnikonorov
Copy link
Member

@brettnolan can you paste the output of pip list please?

@gnikonorov
Copy link
Member

gnikonorov commented Nov 22, 2020

duration comes from the pytest api and must always be in seconds which is why you are getting the error you are seeing.

However, I think it would make sense for pytest-html to do the formatting on your behalf ( I don't really see a reason why 3721 is clearer than 01:02:01, for example )

I'll have a PR up in a day or two for this

@gnikonorov gnikonorov added the enhancement This issue/PR relates to a feature request. label Nov 22, 2020
@brettnolan
Copy link
Author

Thanks!

Here is the 'pip list' output:

Package Version


apipkg 1.5
attrs 20.2.0
certifi 2020.6.20
click 7.1.2
click-completion 0.5.2
colorama 0.4.3
coverage 5.3
crayons 0.4.0
cycler 0.10.0
decorator 4.4.2
execnet 1.7.1
imageio 2.9.0
importlib-metadata 1.7.0
iniconfig 1.0.1
Jinja2 2.11.2
keyboard 0.13.5
kiwisolver 1.2.0
MarkupSafe 1.1.1
matplotlib 3.3.2
more-itertools 8.5.0
networkx 2.5
numpy 1.19.2
opencv-python 4.4.0.42
packaging 20.4
Pillow 7.2.0
pip 20.2.3
pkg-resources 0.0.0
pluggy 0.13.1
py 1.9.0
pycycle 0.0.8
pyparsing 2.4.7
pytesseract 0.3.6
pytest 6.0.1
pytest-cov 2.10.1
pytest-forked 1.3.0
pytest-html 2.1.1
pytest-metadata 1.10.0
pytest-rerunfailures 9.1.1
pytest-xdist 2.1.0
python-dateutil 2.8.1
PyWavelets 1.1.1
scikit-image 0.17.2
scipy 1.5.2
setuptools 50.3.2
shellingham 1.3.2
six 1.15.0
tifffile 2020.9.3
toml 0.10.1
uiautomator 0.3.6
urllib3 1.25.10
zipp 3.1.0

@gnikonorov
Copy link
Member

Hey @brettnolan this functionality is now on the master branch and documentation for it can be found here.

Thanks for raising this issue!

@gnikonorov
Copy link
Member

Hi @brettnolan you can get this functionality by installing the latest version of pytest-html from PyPI

@anil-diwedi
Copy link

anil-diwedi commented Mar 22, 2023

Formatting the Duration Column
The formatting of the timestamp used in the Durations column can be modified by setting duration_formatter on the report attribute. All time.strftime formatting directives are supported. In addition, it is possible to supply %f to get duration milliseconds. If this value is not set, the values in the Durations column are displayed in %S.%f format where %S is the total number of seconds a test ran for.

Below is an example of a conftest.py file setting duration_formatter:

`import pytest

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
setattr(report, "duration_formatter", "%H:%M:%S.%f")`

NOTE: Milliseconds are always displayed with a precision of 2

Documentation Source

@BeyondEvil
Copy link
Contributor

BeyondEvil commented Mar 23, 2023

For future readers of this: The support for duration formatting has been removed in v4.

If there's enough interest in re-implementing support, we will provide a way for you to, via a hook for example, to provide your own formatting functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue/PR relates to a feature request. question This PR is asking whether it is possible to achieve a certain outcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants