Skip to content

Commit 3aa553f

Browse files
authored
fix: Use the same duration formatting as for the tests (#613)
1 parent e6a3d5c commit 3aa553f

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/pytest_html/scripts/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ const renderDerived = (tests, collectedItems, isFinished) => {
7878
const accTime = tests.reduce((prev, { duration }) => prev + duration, 0)
7979
const formattedAccTime = formatDuration(accTime)
8080
const testWord = numberOfTests > 1 ? 'tests' : 'test'
81-
const durationText = formattedAccTime.hasOwnProperty('ms') ? formattedAccTime.ms : formattedAccTime.seconds
81+
const durationText = formattedAccTime.hasOwnProperty('ms') ? formattedAccTime.ms : formattedAccTime.formatted
8282

83-
document.querySelector('.run-count').innerText = `${numberOfTests} ${testWord} ran in ${durationText}.`
83+
document.querySelector('.run-count').innerText = `${numberOfTests} ${testWord} took ${durationText}.`
8484
document.querySelector('.summary__reload__button').classList.add('hidden')
8585
} else {
8686
document.querySelector('.run-count').innerText = `${numberOfTests} / ${collectedItems} tests done`

src/pytest_html/scripts/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const formatedNumber = (number) =>
1+
const formattedNumber = (number) =>
22
number.toLocaleString('en-US', {
33
minimumIntegerDigits: 2,
44
useGrouping: false,
@@ -17,7 +17,7 @@ const formatDuration = ( totalSeconds ) => {
1717

1818
return {
1919
seconds: `${Math.round(totalSeconds)} seconds`,
20-
formatted: `${formatedNumber(hours)}:${formatedNumber(minutes)}:${formatedNumber(seconds)}`,
20+
formatted: `${formattedNumber(hours)}:${formattedNumber(minutes)}:${formattedNumber(seconds)}`,
2121
}
2222
}
2323

testing/test_integration.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ def assert_results(
8383
result = get_text(page, f"span[class={outcome}]")
8484
assert_that(result).is_equal_to(f"{number} {OUTCOMES[outcome]}")
8585

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

9487
def get_element(page, selector):
9588
return page.select_one(selector)
@@ -150,12 +143,43 @@ def test_sleep():
150143
)
151144
page = run(pytester)
152145
duration = get_text(page, "#results-table td[class='col-duration']")
146+
total_duration = get_text(page, "p[class='run-count']")
153147
if pause < 1:
154148
assert_that(int(duration.replace("ms", ""))).is_between(
155149
expectation, expectation * 2
156150
)
151+
assert_that(total_duration).matches(r"\d+\s+ms")
157152
else:
158153
assert_that(duration).matches(expectation)
154+
assert_that(total_duration).matches(r"\d{2}:\d{2}:\d{2}")
155+
156+
def test_total_number_of_tests_zero(self, pytester):
157+
page = run(pytester)
158+
assert_results(page)
159+
160+
total = get_text(page, "p[class='run-count']")
161+
assert_that(total).matches(r"0 test(?!s)")
162+
163+
def test_total_number_of_tests_singular(self, pytester):
164+
pytester.makepyfile("def test_pass(): pass")
165+
page = run(pytester)
166+
assert_results(page, passed=1)
167+
168+
total = get_text(page, "p[class='run-count']")
169+
assert_that(total).matches(r"1 test(?!s)")
170+
171+
def test_total_number_of_tests_plural(self, pytester):
172+
pytester.makepyfile(
173+
"""
174+
def test_pass_one(): pass
175+
def test_pass_two(): pass
176+
"""
177+
)
178+
page = run(pytester)
179+
assert_results(page, passed=2)
180+
181+
total = get_text(page, "p[class='run-count']")
182+
assert_that(total).matches(r"2 tests(?!\S)")
159183

160184
def test_pass(self, pytester):
161185
pytester.makepyfile("def test_pass(): pass")

0 commit comments

Comments
 (0)