summaryrefslogtreecommitdiffstats
path: root/docs/docs/testing.md
blob: 25aad8d691656cc2829893bd238ee4ac40de8d28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Testing

Pendulum provides a few helpers to help you control the flow of time in your tests. Note that
these helpers are only available if you opted in the `test` extra during [installation](#installation).

!!!warning
    If you are migrating from Pendulum 2, note that the `set_test_now()` and `test()`
    helpers have been removed.


## Relative time travel

You can travel in time relatively to the current time

```python
>>> import pendulum

>>> now = pendulum.now()
>>> pendulum.travel(minutes=5)
>>> pendulum.now().diff_for_humans(now)
"5 minutes after"
```

Note that once you've travelled in time the clock **keeps ticking**. If you prefer to stop the time completely
you can use the `freeze` parameter:

```python
>>> import pendulum

>>> now = pendulum.now()
>>> pendulum.travel(minutes=5, freeze=True)
>>> pendulum.now().diff_for_humans(now)
"5 minutes after"  # This will stay like this indefinitely
```


## Absolute time travel

Sometimes, you may want to place yourself at a specific point in time.
This is possible by using the `travel_to()` helper. This helper accepts a `DateTime` instance
that represents the point in time where you want to travel to.

```python
>>> import pendulum

>>> pendulum.travel_to(pendulum.yesterday())
```

Similarly to `travel`, it's important to remember that, by default, the time keeps ticking so, if you prefer
stopping the time, use the `freeze` parameter:

```python
>>> import pendulum

>>> pendulum.travel_to(pendulum.yesterday(), freeze=True)
```

## Travelling back to the present

Using any of the travel helpers will keep you in the past, or future, until you decide
to travel back to the present time. To do so, you may use the `travel_back()` helper.

```python
>>> import pendulum

>>> now = pendulum.now()
>>> pendulum.travel(minutes=5, freeze=True)
>>> pendulum.now().diff_for_humans(now)
"5 minutes after"
>>> pendulum.travel_back()
>>> pendulum.now().diff_for_humans(now)
"a few seconds after"
```

However, it might be cumbersome to remember to travel back so, instead, you can use any of the helpers as a context
manager:

```python
>>> import pendulum

>>> now = pendulum.now()
>>> with pendulum.travel(minutes=5, freeze=True):
>>>     pendulum.now().diff_for_humans(now)
"5 minutes after"
>>> pendulum.now().diff_for_humans(now)
"a few seconds after"
```