Seeds et reproductibilité
Ce qui rend un échec sur une valeur arbitraire exactement rejouable, plutôt qu'un mystère qui change d'apparence à chaque exécution.
Les signatures et les commentaires de documentation ci-dessous restent en anglais sur toutes les pages, dans toutes les locales : ils sont lus directement dans les paquets publiés, et non écrits pour ce site.
sealed class
[Reproducible]
Makes a test's arbitrary values reproducible: the ambient Any context is pinned to a seed for the duration of the test, and that seed is reported only when the test fails — so a red test names the exact seed to replay while a green one stays silent. This is the declarative form of Any.Reproducibly(() => { ... }): the values still vary between runs, which is what surfaces a test secretly depending on one, but a failure is recoverable without the body having been wrapped in advance.
public sealed class ReproducibleAttribute- Hérite de
BeforeAfterTestAttribute- Implémente
IBeforeAfterTestAttribute
Constructeurs
ReproducibleAttribute() |
|---|
Propriétés
int Seed { get; set; } | The seed to pin, to replay a run a previous failure reported. Left unset, every test case draws a fresh seed — the arbitrary-by-default behaviour that surfaces a test depending on one particular value. |
|---|
Méthodes
void Before(MethodInfo methodUnderTest, IXunitTest test) | |
|---|---|
void After(MethodInfo methodUnderTest, IXunitTest test) |
static method
Reproducibly
Runs body with the ambient random context pinned to a fresh seed and, if the body throws, reports that seed before letting the exception propagate. This is how a test that draws on Any stays reproducible: the values still vary between runs (which surfaces accidental dependencies), yet a failure names the exact seed to replay.
static void Any.Reproducibly(Action body, Action<string>? report = null)Méthodes
static void Any.Reproducibly(Action body, Action<string>? report = null) | Runs body with the ambient random context pinned to a fresh seed and, if the body throws, reports that seed before letting the exception propagate. This is how a test that draws on Any stays reproducible: the values still vary between runs (which surfaces accidental dependencies), yet a failure names the exact seed to replay. |
|---|---|
static void Any.Reproducibly(int seed, Action body, Action<string>? report = null) | Replays body with the ambient random context pinned to seed, so a run first seen through the parameterless Reproducibly overload can be reproduced exactly. If the body throws, the seed is reported before the exception propagates. |
static method
ReproduciblyAsync
Asynchronous counterpart of Reproducibly: awaits body under a fresh seed and reports it if the body faults.
static Task Any.ReproduciblyAsync(Func<Task> body, Action<string>? report = null)Méthodes
static Task Any.ReproduciblyAsync(Func<Task> body, Action<string>? report = null) | Asynchronous counterpart of Reproducibly: awaits body under a fresh seed and reports it if the body faults. |
|---|---|
static Task Any.ReproduciblyAsync(int seed, Func<Task> body, Action<string>? report = null) | Asynchronous counterpart of Reproducibly: awaits body under seed and reports it if the body faults. |
static method
UseSeed
Pins the ambient random context to seed until the returned handle is disposed — the scope form of Reproducibly, for a caller that cannot wrap the code it pins in a delegate. A test-framework adapter is the case this exists for: it observes a test through hooks that run before and after it, so it opens the scope in one and disposes it in the other.
static IDisposable Any.UseSeed(int seed)Méthodes
static IDisposable Any.UseSeed(int seed) | Pins the ambient random context to seed until the returned handle is disposed — the scope form of Reproducibly, for a caller that cannot wrap the code it pins in a delegate. A test-framework adapter is the case this exists for: it observes a test through hooks that run before and after it, so it opens the scope in one and disposes it in the other. |
|---|---|
static IDisposable Any.UseSeed(int seed, string replaySnippet) | Pins the ambient random context to seed and supplies the replay snippet — the code a reader copies to replay this run — that generation-failure guidance will embed. This is the form a test-framework adapter uses: the default snippet is Any.Reproducibly(seed, ...), which points at a call a test pinned from outside its own body does not contain, where replaying means changing what the adapter reads instead. |