Skip to content

fix: Use the same duration formatting as for the tests #613

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pytest_html/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ const renderDerived = (tests, collectedItems, isFinished) => {
const accTime = tests.reduce((prev, { duration }) => prev + duration, 0)
const formattedAccTime = formatDuration(accTime)
const testWord = numberOfTests > 1 ? 'tests' : 'test'
const durationText = formattedAccTime.hasOwnProperty('ms') ? formattedAccTime.ms : formattedAccTime.seconds
const durationText = formattedAccTime.hasOwnProperty('ms') ? formattedAccTime.ms : formattedAccTime.formatted

document.querySelector('.run-count').innerText = `${numberOfTests} ${testWord} ran in ${durationText}.`
document.querySelector('.run-count').innerText = `${numberOfTests} ${testWord} took ${durationText}.`
document.querySelector('.summary__reload__button').classList.add('hidden')
} else {
document.querySelector('.run-count').innerText = `${numberOfTests} / ${collectedItems} tests done`
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_html/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const formatedNumber = (number) =>
const formattedNumber = (number) =>
number.toLocaleString('en-US', {
minimumIntegerDigits: 2,
useGrouping: false,
Expand All @@ -17,7 +17,7 @@ const formatDuration = ( totalSeconds ) => {

return {
seconds: `${Math.round(totalSeconds)} seconds`,
formatted: `${formatedNumber(hours)}:${formatedNumber(minutes)}:${formatedNumber(seconds)}`,
formatted: `${formattedNumber(hours)}:${formattedNumber(minutes)}:${formattedNumber(seconds)}`,
}
}

Expand Down
38 changes: 31 additions & 7 deletions testing/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ def assert_results(
result = get_text(page, f"span[class={outcome}]")
assert_that(result).is_equal_to(f"{number} {OUTCOMES[outcome]}")

# if total_tests is not None:
# number_of_tests = total_tests
# total = get_text(page, "p[class='run-count']")
# expr = r"%d %s ran in \d+.\d+ seconds."
# % (number_of_tests, "tests" if number_of_tests > 1 else "test")
# assert_that(total).matches(expr)


def get_element(page, selector):
return page.select_one(selector)
Expand Down Expand Up @@ -150,12 +143,43 @@ def test_sleep():
)
page = run(pytester)
duration = get_text(page, "#results-table td[class='col-duration']")
total_duration = get_text(page, "p[class='run-count']")
if pause < 1:
assert_that(int(duration.replace("ms", ""))).is_between(
expectation, expectation * 2
)
assert_that(total_duration).matches(r"\d+\s+ms")
else:
assert_that(duration).matches(expectation)
assert_that(total_duration).matches(r"\d{2}:\d{2}:\d{2}")

def test_total_number_of_tests_zero(self, pytester):
page = run(pytester)
assert_results(page)

total = get_text(page, "p[class='run-count']")
assert_that(total).matches(r"0 test(?!s)")

def test_total_number_of_tests_singular(self, pytester):
pytester.makepyfile("def test_pass(): pass")
page = run(pytester)
assert_results(page, passed=1)

total = get_text(page, "p[class='run-count']")
assert_that(total).matches(r"1 test(?!s)")

def test_total_number_of_tests_plural(self, pytester):
pytester.makepyfile(
"""
def test_pass_one(): pass
def test_pass_two(): pass
"""
)
page = run(pytester)
assert_results(page, passed=2)

total = get_text(page, "p[class='run-count']")
assert_that(total).matches(r"2 tests(?!\S)")

def test_pass(self, pytester):
pytester.makepyfile("def test_pass(): pass")
Expand Down