Skip to content

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

Description

@brettnolan

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__

Activity

  1. self-assigned this
    on Nov 22, 2020
  2. added
    questionThis PR is asking whether it is possible to achieve a certain outcome
    on Nov 22, 2020
  3. gnikonorov commented on Nov 22, 2020

    @gnikonorov
    Member

    Hi @brettnolan let me look into this for you

  4. gnikonorov commented on Nov 22, 2020

    @gnikonorov
    Member

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

  5. gnikonorov commented on Nov 22, 2020

    @gnikonorov
    Member

    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

  6. brettnolan commented on Nov 23, 2020

    @brettnolan
    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

  7. gnikonorov commented on Nov 27, 2020

    @gnikonorov
    Member

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

    Thanks for raising this issue!

  8. gnikonorov commented on Dec 4, 2020

    @gnikonorov
    Member

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

  9. anil-diwedi commented on Mar 22, 2023

    @anil-diwedi

    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

  10. BeyondEvil commented on Mar 23, 2023

    @BeyondEvil
    Contributor

    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

Metadata

Metadata

Assignees

Labels

enhancementThis issue/PR relates to a feature request.questionThis PR is asking whether it is possible to achieve a certain outcome

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions