Extension for repeatable randomness in tests #5989
|
Something like the random seed display/setter for Does this exist? One potential use case is as a building block for something like Haskell's QuickCheck - the idea is to generate random data and have a predicate that defines if a function's result matches some expectation, shifting focus from setting up test data to the properties a function result should have. Background info The
So if a test fails randomly, one can log the seed, note the one shown when the failure pops up again, and have a repeatable failure because now the random seed is the same. |
Replies: 4 comments 2 replies
|
Both halves of what you're describing already exist in the repo, but they've never been joined — which is a decent argument that your proposal is the missing piece rather than a duplicate. The seed handling you sketched lives in static final String RANDOM_SEED_PROPERTY_NAME = "junit.jupiter.execution.order.random.seed";
static final long DEFAULT_SEED = System.nanoTime();
static Long getSeed(Function<String, Optional<String>> configurationParameterLookup, Logger logger) {
return getCustomSeed(configurationParameterLookup, logger).orElse(DEFAULT_SEED);
}Worth noting for your implementation: that class is package-private and its members are The injection half is already documented, as an example extension rather than shipped API — public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
return pc.isAnnotated(Random.class) && isInteger(pc.getParameter().getType());
}And here's the gap, in the same file: private final java.util.Random random = new java.util.Random(System.nanoTime());It seeds from the clock and neither reports the seed nor accepts one. So the docs show people how to inject randomness while leaving them with exactly the irreproducibility you're trying to fix. That example is probably the strongest thing to point at when you propose this — it's the same shape, minus the half that makes a failure re-runnable. On the QuickCheck angle, expect that to be the part that draws pushback, since jqwik already does property-based testing on the JUnit Platform and maintainers tend to prefer keeping generation strategies out of core. The seed injection stands on its own without it, though — reproducing a flaky failure is useful to anyone using randomness at all, whether or not they're doing property-based testing. I'd lead with that and mention QuickCheck only as a downstream possibility. |
|
Heh. Yes, the whole thing started as "how do I get the test order randomness into my code". After looking into jqwik, I'm saying I'd really like to use that and drop the extension entirely, as it's pretty comprehensive and has anything about randomness one would ever want, and that's just a small building block. |
|
Am 17.08.26 um 11:02 schrieb Phạm Mạnh Lực:
Can't speak to the interest question
Please don't detract from that then.
All the technical points you're raising are valid, already answered,
almost every one of them exactly the way you're suggesting.
But that's the second step.
The first step is finding out whether there's any interest, because
without that, it's not worth doing.
Please consider removing your postings, as they are making it hard for
people with interest in the actual question to find the actually
relevant discussion.
I appreciate the effort you put into them btw.
It's just that they aren't helpful at this stage.
Thanks.
Regards,
Jo
|
|
The JUnit team is pretty helpful here, they'll publish the API to
the extension (including renaming/aliasing the root seed to a name
that better reflects its status).
Note: We still have to discuss that.
Oops. Sorry for misrepresenting this, my bad.
|
Both halves of what you're describing already exist in the repo, but they've never been joined — which is a decent argument that your proposal is the missing piece rather than a duplicate.
The seed handling you sketched lives in
RandomOrdererUtils:Worth noting for your implementation: that class is package-private and its members are
static, sitting inorg.j…