Zac-HD / hypofuzz

Adaptive fuzzing of Hypothesis tests
https://hypofuzz.com/docs
GNU Affero General Public License v3.0
76 stars 3 forks source link

Add covering inputs as `@example(...)`s in the code #13

Closed Zac-HD closed 1 year ago

Zac-HD commented 1 year ago

Here's a neat workflow, combining the benefits for PBT and fuzzing with deterministic tests:

  1. Use the fuzzer to find a reasonably diverse set of covering examples (already works)
  2. Automatically edit them into the code as explicit @example(...) cases (this issue!)
  3. Run your standard CI with only full-explicit deterministic examples (already works; see also https://github.com/python/cpython/pull/22863)

So what will it take to have automatically-maintained explicit examples? Some quick notes:

This seems fiddly, but not actually that hard - we already report covering examples on the dashboard, after all. No timeline on when I'll get to this, but I'd be very happy to provide advice and code review to anyone interested in contributing 🙂

mentalisttraceur commented 1 year ago

So if I understand correctly, the big-picture benefit is to work better with graceful degradation of hypothesis tests to just plain unit tests with hard-coded cases?

By automatically providing those hard-coded cases in a form accessible to stub implementations of the hypothesis APIs which want to stay so simple that they can't even read the example database?

(Besides working around any hassle with making the example database available everywhere, which to me seems better solved by providing a separate solution with great UX for hosting/distributing/syncing the example database.)

Zac-HD commented 1 year ago

Smoothly stepping down to a parametrized unit-test, yes. For some users (e.g. CPython) this is so that they can use a stub implementation; others might be happy to use Hypothesis' phases setting which already supports this!

At scale, say 100+ people and 1M+ lines of code, it can be really important to have fast and deterministic CI because you're testing for regressions rather than bugs via that workflow above, and can run a separate bug-hunting program 'alongside' your CI system. Otherwise I agree that sharing the example database is almost always going to be a better and easier solution, via e.g. Hypothesis' RedisExampleDatabase or just writing your own trivial wrapper around whatever datastore you like.

Zac-HD commented 1 year ago

Turns out that https://github.com/HypothesisWorks/hypothesis/pull/3631 is a rather nice feature for Hypothesis itself, so we'll ship almost all the code upstream and HypoFuzz can just call into the hooks I left😁

Zac-HD commented 1 year ago

OK, we've shipped all the upstreamable internals in Hypothesis - including removal of @example()s with a specific tag - and I have a MVP branch up at https://github.com/Zac-HD/hypofuzz/compare/write-patches. Further notes: