Exemple
freezegun 1.5.5: Freeze the clock in a Python test with freezegun so datetime, time.time and time.monotonic agree, and know what freeze_time cannot reach
Échantillon vérifié pour pypi freezegun 1.5.5: Freeze the clock in a Python test with freezegun so datetime, time.time and time.monotonic agree, and know…
sha256:e0a5abccbe96dde0a46b9b65aae94c3d5ffaa94f5eac58c72b82bec2141a0bfa
Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré.
Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes.
MIT-0
Preuves d'exécution
L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.
- Base de preuve
- Contrat signé réussi
- Reçus de vérification
- 2
- Clés de signature qui l’ont compilé
- 2
Environnement déclaré
python 3.12 linux x64 python 3.12 python pip
Environnements des exécutions de vérification
| Environnement | Contrat | Étapes | Exécution |
|---|---|---|---|
| python 3.12 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · python@1 |
2026-08-14 |
| python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · python@1 |
2026-08-18 |
Cas
HOW- Objectif
- Freeze the clock in a Python test with freezegun so datetime, time.time and time.monotonic agree, and know what freeze_time cannot reach
- Symboles
-
- freezegun.freeze_time
- FrozenDateTimeFactory.tick
- FrozenDateTimeFactory.move_to
- tz_offset
- datetime.datetime.now
- datetime.datetime.utcnow
- datetime.date.today
- time.time
- time.monotonic
- time.perf_counter
- Environnement
- python 3.12
- Créé
- 2026-08-14T13:20:26Z
Contrat
- assert freeze_time as a context manager pins datetime.now(), datetime.utcnow(), time.time(), time.time_ns() and date.today() to one instant that does not tick between reads
- assert the frozen readings agree with each other, so datetime.now().timestamp() is exactly time.time()
- assert a module that did `from datetime import datetime` before the freeze is frozen too, because freeze_time rebinds the name that module already holds
- assert freeze_time rewrites the test file's own globals as well, so a bare handle kept for comparison becomes the fake class and compares equal to it
- assert a bound classmethod captured at import (now = datetime.now) escapes the sweep and keeps returning real time inside the freeze
- assert a module-level constant computed at import (DEFAULT_START = datetime.now()) keeps its real value and its real class, and still passes isinstance against the frozen datetime
- assert a duration measured against that stale constant comes out negative by more than twenty-five years with nothing raised
- assert a datetime subclass defined at import ignores the freeze in now() but follows it in today(), because CPython builds today() on time.time and now() on the C system clock
- assert tick() advances one second and returns the new time, and tick(timedelta) and tick(float) advance further, with time.time() moving alongside
- assert move_to() sets an absolute time, returns None, and carries the module's readings and the subclass's today() with it
- assert time.monotonic and time.perf_counter are frozen to the same wall clock as time.time, so a duration spanning the boundary of the freeze measures about thirty years
- assert moving the frozen clock backwards makes time.monotonic() go backwards
- assert a module first imported inside the freeze keeps the frozen constant, as a FakeDatetime instance, after the block ends
- assert leaving the block restores datetime, the rewritten module globals and the real monotonic clock
- assert tz_offset is added to now() but never to utcnow(), so those two are what differ by the offset
- assert an aware now(timezone.utc) carries the same reading as the naive now() rather than a UTC-correct one, so its timestamp() disagrees with time.time() by exactly tz_offset
- assert the naive now().timestamp() equals time.time(), and date.today() takes the offset so a large enough offset changes the date
- assert tz_offset accepts a timedelta, so an offset that is not a whole number of hours works
- assert datetime.utcnow() raises a DeprecationWarning on python 3.12 in real time and none at all under a freeze
Fichiers
- csx.json
- requirements.txt
- src/__init__.py
- src/clock.py
- src/late_import.py
- test/contract.py