70 points · by Smaug123 · 2y ago · 52 comments
Here is an informal testing maturity ladder in increasing order:
- code can only be tested with an integration test
- code is tested by comparing the stdout with an earlier version (hopefully it’s deterministic!)
- code is retrofitted with tests in mind
- code is written to be testable first, maybe with lots of mocks
- code is testable, and pure functions are tested with unit tests, leaving the rest to integration tests. Fewer mocks, some stubs.
- property based tests, assuming the unit tests are fast in the first place
- fuzzing
- mutation based testing
Not speaking of formal specs or performance testing or anything else.
* integration and unit tests: since these are manually written, they scale poorly but are simple.
* property tests: since these are semi-automatic they scale better but are a bit more complicated to setup.
* fuzzing: almost fully automatic, although I don't differentiate this much from property-based testing.
* mutation based testing
Also, mutation based testing is really orthogonal to the others, since it's a way of evaluating the adequacy of the tests, not of actually testing. One could easily imagine using PBT/fuzzing to generate (and then simplify) tests with the express goal of killing mutants.
Do you have any links to these studies?
> * property tests: since these are semi-automatic they scale better but are a bit more complicated to setup.
Once you are in the groove, I find property based tests to be simpler (or not harder) than example based tests. But that's when I am writing tests as I am developing the system, ie I take testability into account in the design.
If you mean what I think I mean, this is the bottom rung of the ladder. Code that is only testable with lots of mocks is in practice worse than code with no tests.
Tests should do two things: catch undiscovered bugs and enable refactoring. Test mocked to high heaven do one thing: confirm that the code is written the way it’s currently written. This is diametrically opposed to and completely incompatible with those two stated goals. Most importantly, the code can’t be changed without breaking and rewriting tests.
Mocks are okay for modeling owners of external state. Even better are dummy/fake implementations that look and behave like the real thing, (but with highly simplified logic).
Perhaps there is also a level -1, when the tests actually make things worse. I see this when tests are extremely brittle, flaky, don't test the most complex or valuable bits of code, are very slow to run, or unmaintained with a list of "tests we know fail but haven't fixed".
I've seen tests with bugs in them, hiding bugs in the code and giving a false sense of robustness. because, you know, code coverage is 100% so we cannot have any bugs, right?
I had to work with tests that test many trivial things, tightly coupled with implementation details. Those would discourage small refactorings, because it took a lot of time to understand and fix the failed tests as changes in details of implementation would lead to failures in unittests. It also slowed down the pace of development for no good reason.
Unless a test has a value that exceeds the cost, it is a net negative.
Some code only makes sense to test with integration tests. It's not more effective with a code base where somebody has decided to fatten up the SLOC with dependency inversion just so that they can write some unit tests which test that x = x.
>- code is tested by comparing the stdout with an earlier version (hopefully it’s deterministic!)
Making the code deterministic or adapting the tests to accommodate that it isn't should be next on the ladder, not hoping that it is.
This is difficult, but one technique that (might) make it easier for real-world applications beyond simple invariants is to take the approach of building a simple model of the system under test and, in the PBT, checking that your system's behavior matches the model's [1].
Especially useful when you want to test an optimized version against a simpler (but slower) baseline version. Or when you have a class of special cases, that you can solve in a simpler way.
Testing the system against itself, but under symmetry, is also useful. But that goes close to general properties. A symmetry could be flipping labels around, or shuffling input order etc; it depends on your system.
But adding mutation testing tends to be harder: it's not just an extra thing you can add in without changing any existing code, it's testing whether the tests are useful in the first place. Which means when you introduce it, you'll probably need to spend some time fixing everything that's currently wrong. This makes it a next step in the testing process beyond just adding a new technique to your existing repertoire.
That said, I've used PBT for a while and not had as much success with mutation testing, so maybe this is a personal bias.
> But adding mutation testing tends to be harder: it's not just an extra thing you can add in without changing any existing code
But it is...
> it's testing whether the tests are useful in the first place.
Not useful. Complete. MT checks that you have tests for all the behavior of the code.
Yes, but there's some easy targets:
Your example based tests often have some values that are supposed not to matter. You can replace those with 'arbitrary' values from your property based testing library.
Another easy test that's surprisingly powerful: just chuck 'arbitrary' input at your functions, and check that they don't crash. (Or at least, only throw the expected errors.) You can refine what 'arbitrary' means.
The implied property you are testing is that the system doesn't crash. With a lot of asserts etc in your code, that's surprisingly powerful.
It's quite straight forward to do MT in software. I've done quite a bit of it for specific libraries that I've built (iommi, tri.struct, and internal code). A big advantage in my book of MT over PBT is the much lower cognitive overhead for MT and that you can know you have done it completely. The second may be just a false sense of security or an emotional blanket, but still.
I have written about mutation testing a few times: https://kodare.net/2019/04/10/mutation-vs-property-based-tes... https://kodare.net/2018/11/18/the-missing-mutant-a-performan... https://kodare.net/2016/12/12/mutation-testing-in-practice.h... and my talk on mutmut from pycon sweden is on youtube: https://www.youtube.com/watch?v=fZwB1gQBwnU
In general it doesn't require language-level support, of course - you can just make a change and rebuild it, à la Stryker https://stryker-mutator.io/docs/stryker-net/technical-refere... . PITest operates on JVM bytecode (https://pitest.org/) for orders of magnitude speedup.
Integration tests must be much higher on the list.
Also: nothing is stopping you from running your prop tests in integration tests, too
Adding property tests into integration tests will make things slower and flakier.
Treating flakiness in integration tests as a fait accompli is also a bad idea. It's a bad idea - a bit like treating flakiness in the app itself as a fait accompli.
If running tests with different data makes them flaky, your system is bad.
Regardless of property based testing, I've seen too many systems where there were hundreds of unit tests and which wold fail for the most trivial reasons because the actual integration of those units was never tested properly.
https://research.chalmers.se/publication/517894/file/517894_... https://www.youtube.com/watch?v=G0NUOst-53U
He gives fantastic guidance about how to write PBTs.
One thing I find that people struggle with is coming up with "good properties" to test with.
That's the wrong way to think about it. The properties you want to test are the function's contract. Checking that contract is the goal.
You can be as specific with the contract as you want. The more specific the more bugs you'll find.
Property-based tests are just one way to check the contract, as are hand-written unit tests. You could use a static analyzer or a model checker as well, they're all different approaches to do the same thing.
EDIT: by contract I mean the guarantees the function imposes on its output. A contract for a sorting function could be as simple as the length of the output being the same as the input. That's one property. Another is that every element in the output is also in the input. You can go all the way and say that for every element at index i, the element at index i+1 (if any) is larger.
But you don't need a perfect contract to start with nor to end with. You can add more guarantees/properties as you wish. The more specific, the better (but also slower) the tests.
I don't mean to say that PBT is a bad idea. I actually think it's very good. I wish there was a way to make it really useful though. As the paper mentions, PBT in their experience is used for "component testing", which is just another name for "unit testing", which is where automation of this kind isn't all that important. Integration and E2E testing is what's a lot more important, but doesn't really have a way to approaching r.n.
Ugh...
> The P compiler is implemented in C# and hence the tool chain requires dotnet.
Nah, sorry. I'm not going to read further about it.
And their own compiler. https://github.com/ocaml-flambda/ocaml-jst
Not to be negative about hypothesis though, I like it, tried to help increase adoption at my previous job after a colleague introduced me, but that was shortly after Pycon UK 2018/19 and I haven't used it since. Not that I don't think we'd benefit from it, just probably lower hanging fruit atm.