Aller au contenu
Skillpedia
Tests8.0k · #12694

property-based-testing

Ce qu’il fait

Guide les tests basés sur des propriétés, où un test énonce une règle pour toutes les entrées et un outil cherche des contre-exemples. Il s'adresse aux développeurs qui veulent des tests couvrant bien plus qu'une liste d'exemples choisis à la main.

Quand l’utiliser

  • ✓Quand un développeur veut des tests qui couvrent une large plage d'entrées, et non quelques exemples.
  • ✓Quand un développeur teste une paire encodeur-décodeur, un analyseur ou une fonction de tri.
  • ✓Quand un développeur veut vérifier un invariant de contrat Solidity avec un outil de fuzzing.

Lien du skill

https://github.com/trailofbits/skills/blob/main/plugins/property-based-testing/skills/property-based-testing/SKILL.md

Copiez-le et collez-le dans votre IA (Claude, ChatGPT ou Cursor) en lui demandant de suivre les instructions.

Comment l’utiliser

Le plus simple : copiez le message et collez-le dans votre IA. Elle lit le skill et suit les instructions.

Message prêt à coller

Utilise ce skill pour m’aider : https://github.com/trailofbits/skills/blob/main/plugins/property-based-testing/skills/property-based-testing/SKILL.md Lis le fichier SKILL.md de ce lien et suis ses instructions pour ma tâche.
Envie que votre IA trouve seule des skills comme celui-ci ?Installez le MCP gratuit de Skillpedia : une ligne, et elle cherche parmi 15 000+ skills avant chaque tâche.Installer en 1 minute →

Exemple de demande à votre IA

“Écris des tests basés sur des propriétés pour mon encodeur et décodeur JSON, pour qu'ils vérifient chaque aller-retour.”

5 nouveaux skills par semaine. 1 minute de lecture.

Comment ça marche

Le skill repère des propriétés dans le code, comme les allers-retours, l'idempotence ou les invariants, et écrit des tests qui les énoncent. Il vérifie si le code a une forme claire à tester et suggère une refactorisation si ce n'est pas le cas.

Contenu technique (SKILL.md original)

Texte original de l’auteur, en anglais, directement depuis GitHub.

Property-Based Testing

An example test asserts one point. A property asserts a rule over the whole input domain and lets the generator hunt for the counterexample. That trade is worth making when the code has an algebraic shape — an inverse, an invariant, an oracle — and not otherwise. Code with no such shape gets example tests; saying so is a valid outcome.

Check first whether the shape is missing or merely buried. A calculation wrapped in I/O, a string built by concatenation, an in-place mutation — each has a property and no seam to assert it through. See references/refactoring.md before concluding there is nothing to assert.

Property catalog

Property Formula Where it applies
Roundtrip decode(encode(x)) == x Serialization, conversion pairs
Inverse f(g(x)) == x encrypt/decrypt, compress/decompress
Oracle new(x) == reference(x) Optimization, refactoring, reimplementation
Idempotence f(f(x)) == f(x) Normalization, formatting, sorting
Invariant Holds before and after Any transformation, contract state
Easy to verify is_sorted(sort(x)) Complex algorithms with cheap checkers
Commutativity f(a, b) == f(b, a) Binary and set operations
Associativity f(f(a,b), c) == f(a, f(b,c)) Combining operations
Identity f(x, e) == x Operations with a neutral element

Strength ordering, weakest to strongest: no crash → type preservation → invariant → idempotence → roundtrip / oracle.

Assert the strongest property the code supports. "No crash" alone rarely justifies the dependency — if that is all you can find, either a small rearrangement exposes something stronger, or the honest report is that this code is a poor PBT candidate. Rule out the first before settling for the second.

The two ways a property test asserts nothing

  • Tautology. assert add(a, b) == a + b restates the implementation; no bug they share can fail it. Pick a property that constrains the function without recomputing it. Note the exception: f(x) == f(x) is a genuine determinism property when f is not obviously pure — serializers over dicts or sets, hashing, anything reading the clock.
  • Vacuity. assume() that filters out nearly every input passes without exercising anything, and self-contradictory assume() passes having run zero cases. Push constraints into the strategy so the generator produces valid inputs directly.

Where to look next

Load the one that matches the task in front of you:

Task File
Writing new tests, designing strategies references/generating.md
The code has no property to assert yet references/refactoring.md
Reviewing existing property tests references/reviewing.md
A property test just failed references/interpreting-failures.md
Library choice, Echidna and Medusa references/libraries.md

Introducing PBT to a project that lacks it

If the project already uses a PBT library, just write the tests in it. If it does not, adding one is a dependency decision that belongs to the user — offer it once with the specific property you would write, and take the answer either way.

Questions fréquentes

Est-ce meilleur que des tests d'exemples ?

Pas toujours. Le skill indique qu'un code sans règle claire à vérifier doit conserver des tests d'exemples.

Quels outils couvre-t-il ?

La description cite Hypothesis, fast-check, proptest, jqwik, rapid, Echidna et Medusa.

Qu'est-ce qu'une propriété ?

C'est une règle qui doit être vraie pour toute entrée valide, par exemple un décodage qui restitue la valeur d'origine après encodage.

Détails

Description originale de l’auteur

Writes, reviews, and debugs property-based tests — Hypothesis, fast-check, proptest, jqwik, rapid, and Echidna or Medusa for Solidity invariants. Use whenever tests should cover a whole input domain instead of a hand-picked list of examples: encode/decode and serialize/deserialize pairs, parsers, canonicalizers and normalizers, validators, numeric and Decimal types, comparators and sort order, data structures, and smart-contract state invariants. Also use when adding cases to an existing @given, fast-check, or proptest suite, when judging whether existing property tests assert anything real, a

repo: trailofbits/skills

Popularité

Étoiles GitHub
8.0k
Position au classement
#12694
Licence
CC-BY-SA-4.0
Mis à jour
02/11/2026

Ce skill vous est-il utile ?

0

Connectez-vous pour voter.

Commentaires

  • …

Autres skills de ce dépôt

Skills similaires