You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
new internal helper classes for date times and time deltas
meaning of relative for time-deltas
decision on whether timestamps can have a meaningful rel at all ( i currently lean on no)
documentation of the explicit requirements for declaring approximate date-times in nested structures (no implication as we don't have sane defaults for abs/rel)
classapprox_datetime(ApproxBase):
""" Perform approximate comparisons where the expected value is a datetime or timedelta. """def__init__(self, expected, abs: timedelta=timedelta(seconds=10)):
ifabs<timedelta(0):
raiseValueError(f"absolute tolerance can't be negative: {abs}")
super().__init__(expected, abs=abs)
def__repr__(self):
returnf"approx_datetime({self.expected!r}\u00b1{self.abs!r})"def__eq__(self, actual):
returnabs(self.expected-actual) <=self.abs
Reacted by Micael Jarniac, Tal Bortman, Walter Cloete and Ignition
Is there any way to get the time that the current test was started from pytest? Most of the time when I'm doing these approx date comparisons, it's when the result of a test might be out by the length of time that something in the test took to run. If you could say assert abs(expected - actual) <= time_this_test_has_been_running that would catch most of my use cases without having to concoct a sensible default tolerance... 🤷♀️
Came to me as I was bumbling around the task myself, so thought I would capture here as part of the discussion. Great library 🙌
@bentilley one way to do that would be to create a function-scoped fixture which returns the current time. The fixture will be initialized with the current time immediately before each test runs:
importtime, pytestdefapprox_now(abs=10):
"""Pytest based checker for whether a unix epoch/timestamp is approximately 'now' (within some tolerance)."""returnpytest.approx(time.time(), abs=abs)
deftest_something():
# ....assertmetadata== {"status": "running", "timestamp": approx_now()}
Reacted by TimoRJensen, Alexander Schepanovski and BLTHDJ
Activity
brianmaissy commentedon Mar 4, 2021
FWIW, this is what I'm currently using:
RonnyPfannschmidt commentedon Mar 4, 2021
Thanks for the reference, thats a good start point
selting commentedon Mar 26, 2021
Would love to see this feature added!
bentilley commentedon May 11, 2021
Is there any way to get the time that the current test was started from pytest? Most of the time when I'm doing these approx date comparisons, it's when the result of a test might be out by the length of time that something in the test took to run. If you could say
assert abs(expected - actual) <= time_this_test_has_been_runningthat would catch most of my use cases without having to concoct a sensible default tolerance... 🤷♀️Came to me as I was bumbling around the task myself, so thought I would capture here as part of the discussion. Great library 🙌
brianmaissy commentedon May 11, 2021
@bentilley one way to do that would be to create a function-scoped fixture which returns the current time. The fixture will be initialized with the current time immediately before each test runs:
bentilley commentedon May 11, 2021
@brianmaissy That is a great shout 🙏 thank you
vallsv commentedon Jun 21, 2021
Supporting approx fordatetimeandtimedeltasounds very interesting. Any idea why it is not already merged? Is there technical issues?Oups, i though it was a PR, not an issue.
vallsv commentedon Jun 21, 2021
BTW, i don't know if it is useful, but this can be reduced as float comparison:
soxofaan commentedon Jan 19, 2022
Just FYI, a related helper I've been using:
Seluj78 commentedon Dec 29, 2023
This is still a very much needed feature, is there any news on this ?
Add datetime/timedelta support to pytest.approx (pytest-dev#8395)
Add datetime/timedelta support to pytest.approx (pytest-dev#8395)
Add datetime/timedelta support to pytest.approx (pytest-dev#8395)
2 remaining items
Add datetime/timedelta support to pytest.approx (pytest-dev#8395) (py…
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#340)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#454)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#225)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#263)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#590)
chore(deps): update dependency pytest to <9.1.1,>=9.1.0 (#456)
⬆️ Update dependency pytest to v9.1.1 (#24)
[SECURITY] Update dependency pytest to v9 [SECURITY] (#2)
test: Pass an explicit tolerance when comparing timedeltas
Reimplement `approx_time` on top of `pytest.approx()`
Reimplement `approx_time` on top of `pytest.approx()`
chore(deps): update root dev dependencies to v9.1.0
Update dependency pytest to v9 (#35)