6 ms·
Property-Based Testing for the People
- cosmic_quanta 2y agoThis work was discussed by the author in the Haskell Interlude podcast as well [0]. Highly recommended and probably easier to digest than a whole dissertation. [0]: https://haskell.foundation/podcast/59/ https://haskell.foundation/podcast/59/
- sunesimonsen 2y agoI think property based testing becomes a lot easier when you can just use normal asserts like this: https://github.com/unexpectedjs/unchecked https://github.com/unexpectedjs/unchecked
- DarkNova6 2y agoNot to be too critical but coming from Java/C# I‘m not not so hot they are writing their own tests: https://github.com/unexpectedjs/unchecked/blob/master/test/index.spec.js https://github.com/unexpectedjs/unchecked/blob/master/test/i... Maybe just a matter of familiarity?
- hitchstory 2y agoProperty testing is a lot like formal methods - really cool, but almost entirely useless in ~95% of commercial contexts. They're both extremely useful when, say, building a parser, but when the kind of code you write involves displaying custom widgets, taking data and pushing it onto a queue, looking up data in a database, etc. integration tests have a lot more bang for the buck.
- diggan 2y agoI've found it effective for anything that handles arbitrary input, especially from end-users. But if that data is coming from within your systems where you have full control over everything, less valuable.
- hitchstory 2y agoIf the arbitrary input is, say, a text box which takes a name and puts it into a database, it probably won't uncover any bugs. It has some use if you build something like a complex pricing engine, numerical code or a parser for a mini DSL. I find that problems of this type don't crop up a lot though.
- IanCal 2y agoI disagree. I've used it for things like "regardless of where you are on the page, tab n times and shift tab n times leaves you on the original item". I found a bug in our tv ui library which was actually a bug in the spec. Regardless of how you built the ui, if you press a direction and focus moves, pressing the opposite direction takes you back - but we had another rule that broke this. We had tests for both, and it was only when I made the general test (for all ui, items in it and directions) it found the inconsistency. It was also pretty easy to write. I've also found issues around text processing due to lowercasing not always resulting in the same length string and more. I found a bug demoing pbt for a contact gig I was going for that was around some versioning. To be honest I've never implemented it, even for a demo, and not found a bug.
- josephg 2y ago> To be honest I've never implemented it, even for a demo, and not found a bug. Me too. I tend to roll my own property testers / fuzzers per project instead of using a library. But my experience is similar to yours. Out of maybe 25 testers, I think the only times I didn’t find any bugs was when I messed up the tests themselves. It’s incredibly humbling work.
- IanCal 2y agoI used to build these too, my example was one in actionscript (always fun on restricted devices) and it was a bug in a library I wrote. I was actually testing the property tester and found the bug then, after realizing it wasn't a bug in the new test/tester. > It’s incredibly humbling work Absolutely. Glad others feel it too. I almost feel like you need a decent reason why not to use them in places. Ideally to can describe the behaviour of something in general terms around what stays true. If you can't, is it too hard for people to understand? I will however say I don't usually write pbts but have a strong feeling I should add more.
- boscillator 2y agoIt's very useful when you're working on numerical software. Often, it's hard to figure out exactly what output your code should return (because if you knew the answer you wouldn't have to write the code), but you can easily list properties you expect.
- matt_d 2y agoRight, metamorphic testing in particular (which would be a special case of PBT, with metamorphic relations being properties), https://en.wikipedia.org/wiki/Metamorphic_testing https://en.wikipedia.org/wiki/Metamorphic_testing, https://github.com/MattPD/cpplinks/blob/master/testing.md#property-based-testing-metamorphic https://github.com/MattPD/cpplinks/blob/master/testing.md#pr... One simple example (from the above) is "sin (π − x) = sin x" for the implementation of the sine function not requiring the knowledge of its specific output values. Naturally, instead of the literal equality "=" one can use a more appropriate accuracy specification as in, say, relative ulp (https://en.wikipedia.org/wiki/Unit_in_the_last_place https://en.wikipedia.org/wiki/Unit_in_the_last_place) error bound, cf. https://members.loria.fr/PZimmermann/papers/accuracy.pdf https://members.loria.fr/PZimmermann/papers/accuracy.pdf
- thehappyfellow 2y agoHow come e.g. Jane Street uses it so much? It’s the second most common type of test I write.
- hansvm 2y agoThe same reason Google burns $50M+ in electricity each year using protobufs instead of a more efficient format. An individual company having specific needs isn't at odds with a general statement being broadly true.
- thehappyfellow 2y agoHow’s that comparable at all? There are no network effects from writing property based tests, people use them if they are helpful - are they testing enough of the code with reasonable amount of effort. Nobody’s forcing people to write tests, unlike Google forces usage of protobuf on all projects there.
- hansvm 2y agoIt's comparable in the way described in sentence #2: > An individual company having specific needs isn't at odds with a general statement being broadly true. Google needs certain things more than reduced carbon emissions, and Jane Street needs certain things more than whatever else they could spend that dev time on.
- thehappyfellow 2y agoFine but cutting the thought process at "it depends" is not a great way to understand what's happening here. You can explain anything happening at any company by saying "they need certain things more than whatever else they could spend that time on". Why is PBT useful at Jane Street, at least more than in other places? Is it the use of functional language? Average Jane Street dev being more familiar with PBT? Is the domain particularly suited to this style of testing? Explicitly, my claim is that the biggest bottleneck is education on how to use PBT effectively and Jane Street is not using them to get an extra mile in safety, they use it because it's the easiest way to write large chunk of the tests.
- choeger 2y agoHah! Try to separate your domain logic from your interfaces (e.g., using something like hexagonal architecture) and then say this again. Yes, it's a lot of work coming up with good properties, but it massively helps to find gaps in the domain logic. In my experience, these gaps are what's typically expensive, not the weird problem a junior had with properly using Redis or S3.
- hitchstory 2y ago>Hah! Try to separate your domain logic from your interfaces Im not an amateur. The only time I dont do this is when there literally is no domain logic yet (e.g. a CRUD app). >In my experience, these gaps are what's typically expensive, not the weird problem a junior had with properly using Redis or S3. What can I say? Your experience might not be as broad as mine. Redis is a source of almost no bugs because it is very well designed, but most interfaces I couple to have design qualities that are the exact opposite of redis's. Those interfaces (e.g. wonky payment gateway APIs, weird microservice APIs) are the probably source of most bugs in enterprise systems I work on. #2 is probably simple misspecifications (customer said code should do X, it should actually do Y which is almost the same but very slightly different). #3 would be domain logic errors. And even most of those are uncovered and avoided with saner architecture or a couple of unit tests. For the parsers I write at home, sure, property testing kicks ass. For your college degree algorithm coursework, sure, it helps a lot. For 95% of business logic? Pointless, the complexity isnt buried deep in the business logic.
- thom 2y agoI agree, but this is a good thing! My default approach these days is functional tests for everything possible, and property based tests for anything particularly algorithmic or containing lots of edge cases, and no unit tests outside that. This is a great combo, covers all the business value without leaving obscure bugs, and also isn’t a pain every time you refactor.
- amw-zero 2y agoFor "business applications," I much prefer the variant of property testing called model-based testing. This is where your property is "does the implementation behave like some simplified model." This is the correct way to test such applications in my opinion, because users don't care about "custom widgets," "queues," or "databases." They care about business functionality, which is quite easily expressible in model form. I don't do much mocking either, because I also prefer integration tests for these types of applications. Here is an example that tests against a database, all the way up to the state management layer of the UI: https://concerningquality.com/model-based-testing/ https://concerningquality.com/model-based-testing/. In fairness, people don't write about doing this a lot. PBT literature seems to revolve around sorting functions or binary tree operations. But PBT much more powerful than just that.
- hitchstory 2y ago>in my opinion, because users don't care about "custom widgets," "queues," or "databases." Users care quite a lot when these things break. >They care about business functionality, which is quite easily expressible in model form. They care about reliable applications that do the task they want. Users don't have a concept of "business functionality". If a widget they wanted to use is broken, they'll be pissed - they won't tell you that it's ok because the concept of the application was still good. From your link: >The main thing that I want to highlight here is how simple the model is. This is pure domain logic, and is probably as close to the essential complexity of the application that we can get This is exactly the issue. If I go down my recent list of bugs and categorize them, ~8 out of 10 of them are in areas which the author would have *abstracted away*. The other two probably could have been avoided by having a team member write one test to cover that functionality instead of zero. What does it mean when you abstract something away from a test? Well, it means the test won't catch a bug in that thing. If I went down my recent list of bugs and been able to see that 9 out of 10 of them would have been caught via adding property testing/model testing on top of regular testing/typing, etc. I'd have absolutely no second thoughts about implementing it but that simply isn't the case. I have worked on problems in (not usual) environments where it is the case, and in those rare cases I have advocated for and used property testing but I have more often seen it advocated and used by people who see every bug as a nail and property testing as some kind of super advanced hammer.
- LAC-Tech 2y agoI have to admit that after all these years the term "integration tests" is near meaningless me. But proberty based testing is absolutely real. If you can think of one example based test you can probably think of a way to generalise them. It's a powerful idea, much more powerful than trying to remember what exactly a "unit" means.
- 082349872349872 2y agoFor structure generation I prefer Doug McIlroy's approach: pick a tree size (from some arbitrary distribution), and then, of the n possible valid structures of that size, produce the kth one uniformly. https://www.cs.dartmouth.edu/~doug/nfa.pdf https://www.cs.dartmouth.edu/~doug/nfa.pdf gives an nfa variant; extending to a pda is an (interesting, I found) exercise.
- dpc_01234 2y agoWould be very valuable if someone could write a summary of novel ideas for practitioners (if there are any).
- chriswarbo 2y agoNot read the whole thing yet, but the idea to represent generators as free monads which can be interpreted in multiple ways is very promising. In particular, this may be a way to unify various generator implementations, like the random generators from QuickCheck, hedgehog, falsify, SmartCheck, etc. with the enumerations provided by Smallcheck, FEAT, LeanCheck, etc. and maybe even the weirder ones like LazySmallcheck, extrapolate, fitspec, speculate, lazy-search, spectacular, the various logic-programming implementations of property-checking, etc. In principle, the same framework would extend to parsers too (atto/mega/parsec, etc.), though parsing is often a bottleneck in real applications, so many would avoid such abstraction/indirection. Still, it would be nice if libraries could define parsers for their datatypes, in such a way that it's easily reusable as a generator in tests (without having the library package depending on any test-generator packages). They also demonstrate finding the "derivative" of a generator (i.e. the result of fixing its next choice to a particular outcome), which is useful for avoiding choices that cause preconditions to fail (something which many PBT users seem to struggle with: assuming that the framework will somehow avoid problematic values, rather than just rejection-sampling them as most do).
- chriswarbo 2y agoOne of the later chapters describes the Tyche tool, which seems to be available as this VisualStudio(Code?) extension https://marketplace.visualstudio.com/items?itemName=HarrisonGoldstein.tyche https://marketplace.visualstudio.com/items?itemName=Harrison... and has a standalone version at https://tyche-pbt.github.io/tyche-extension/ https://tyche-pbt.github.io/tyche-extension/ It reads JSON data (described in the paper), which can be generated during property checking, e.g. using https://github.com/tyche-pbt/tyche-quickcheck https://github.com/tyche-pbt/tyche-quickcheck or a Hypothesis equivalent.
- tomnicholas1 2y agoThe python package Hypothesis[0] already does a great job bringing property-based testing to the people! I've used it and it's extremely powerful. [0]: https://github.com/HypothesisWorks/hypothesis https://github.com/HypothesisWorks/hypothesis
- epgui 2y agoI have used Python's `hypothesis` as well, and I wish it were better. We had to rip it out at work as we were running into too many issues. I have also used Haskell's `QuickCheck` and Clojure's `spec` / `test.check` and have had a great experience with these. In my experience they "just work". Conversely, if you're trying to generate non-trivial datasets, you will likely run into situations where your specification is correct but Hypothesis' implementation fails to generate data, or takes an unreasonable amount of time to generate data. Example: Generate a 100x25 array of numeric values, where the only condition is that they must not all be zero simultaneously. [1] [1] https://github.com/HypothesisWorks/hypothesis/issues/3493 https://github.com/HypothesisWorks/hypothesis/issues/3493
- mrcsd 2y agoCare to expand upon the issues you were running into with hypothesis? I'm genuinely curious as I may soon be evaluating whether to use it in a professional context.
- rtpg 2y agoI understand your pain in some sense, but on another I feel like people with a decent amount of hypothesis experience "know" how the generator works and would understand that you basically _never_ want to use `filter` if you can avoid it, instead relying on unfalsifiable generation. Silly idea for your generator would to generate an array, and if it's zero... draw a random index and a random non-zero number and add it into the array. Leads to some weird non-convexity properties but is a workable hack. In your own example you turned off the "data too slow" issue, probably because building up a dataframe (all to just do a column sum!) is actually kind of costly at large numbers! Your complaint is probably actually meant for the pandas extras (or pandas itself) rather than the concept of hypothesis.
- choeger 2y agoNice work. I didn't yet read it fully, but I love the idea. Looks to be a valuable thesis.
- mont_tag 2y ago> My work is motivated by conversations with real PBT users, accentuating the benefits that they get from PBT and reducing the drawbacks. ISTM that the benefits and drawbacks are just two sides of the same coin. The benefit is reduced testing effort. The drawback is that savings comes from sacrificing knowledge of exactly what has been tested. > I also observed that PBT users are not always good at evaluating whether their testing was effective. Ah, there's the rub. It seems that the benefit of making the tested property explicit is partially offset by not really knowing how much of the search space was explored automatically.
- chriswarbo 2y ago> The benefit is reduced testing effort. I wouldn't agree with this assessment. PBT has frequently surprised me, finding bugs that I never would have thought to test. It's not merely a tool for generating the unit tests we would have written anyway; it's a way to write down how we think the parts of a system interact, and have the machine show us that we're wrong. Crucially, it's quite hard to prove ourselves wrong, since it's hard to think up scenarios that we hadn't considered, so it's nice to have the machine help with that part. > The drawback is that savings comes from sacrificing knowledge of exactly what has been tested. I encourage you to use the statistics-gathering features that many (most?) PBT systems provide (e.g. functions with names like "collect", "label", etc.). Also, whilst preconditions/filtering can be useful in a pinch, it's usually a good idea for a property to construct data of the appropriate sort itself (e.g. using arbitrary arguments to fill in an appropriate template); rather than relying on a completely arbitrary generator to hit scenarios you care about.
- thehappyfellow 2y agoI think the surprise part in under-appreciated. The quicktest tests I write are frequently of the form “let’s spend 15 minutes and see what happens” kind of tests - and I almost always find bugs that way. The return on investment is bonkers. I do think that immutable by default OCaml + good PBT tooling there helps a lot.
- algorithmsRcool 2y agoJust going to plug the excellent .NET PBT library, CsCheck [0]. I have used it quite a bit to excersize strange corners of my program logic and found several exotic bugs with it. [0]: https://github.com/AnthonyLloyd/CsCheck https://github.com/AnthonyLloyd/CsCheck
- erichocean 2y agoLLMs are great at generating property-based tests.
- fouc 2y agoI read that as "Property-Based Testing of People"
- pfdietz 2y agoIn the testing I focus on, testing of compilers, and in particular Common Lisp implementations, PBT has been invaluable, finding bugs in every implementation on which it was tried (I understand this is a universal experience in compiler testing, for example seen from Csmith on C compilers). Here, the property is often of the form "these two related pieces of code should not crash and also compute the same thing". As a simplified example, an expression like (+ x y) should compute the same thing as (the <type1> (+ (the <type2> x) (the <type3> y))) where the types are randomly generated types that contain the values in question.
- bossyTeacher 2y agoMy main issue with that approach is that your tests become flaky due to random data generation in your tests. They can be fine in your staging build pipeline and fail at your prod build pipeline. Extensive logging doesn't change that fact.
- chriswarbo 2y agoYou can fix the PRNG seed in your build pipelines, which should make them execute the same examples for each test. Hypothesis has a nice feature which uses a hash of each test's source code as the PRNG seed for that test, which ensures the same examples are checked every time (unless someone changes the text of that particular test). It's a nice idea, but might be hard to implement in compiled languages (like Haskell, for example).