Skip to content
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
8 changes: 4 additions & 4 deletions pendulum/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ def _sign(x):
@contextmanager
def test(mock):
set_test_now(mock)

yield

set_test_now()
try:
yield
finally:
set_test_now()


def set_test_now(test_now=None):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,20 @@ def test_week_ends_at_invalid_value():

with pytest.raises(ValueError):
pendulum.week_ends_at(11)


def test_with_test():
t = pendulum.datetime(2000, 1, 1)

with pendulum.test(t):
assert pendulum.now() == t

assert pendulum.now() != t

# Also make sure that it restores things after an exception
with pytest.raises(RuntimeError):
with pendulum.test(t):
assert pendulum.now() == t
raise RuntimeError

assert pendulum.now() != t