0

I have a function with the following structure.

def foo(*args, **kwargs):
    # ... code
    now = pd.Timestamp('now', tz=0)
    # ... more code
    return result # result depends on "now" variable

I cannot take a timestamp right before or after the execution of foo and use it for comparisons against the return value, because it will differ from the value of now inside the function.

actual_panda
  • 1,178
  • 9
  • 27

1 Answers1

2

You can use a time mocking library, e. g. https://pypi.org/project/freezegun/ to freeze time to a known instant in your test.

def test_foo():
    with freezegun.freeze_time("2021-04-14"):
        result = foo()  # assert that result is correct for the above date
AKX
  • 152,115
  • 15
  • 115
  • 172