I was inspired by the jane street post and implemented exactly this in my Scala unit testing library uTest (<a href="http://www.lihaoyi.com/post/GoldenLiteralTestinginuTest090.html" rel="nofollow">http://www.lihaoyi.com/post/GoldenLiteralTestinginuTest090.h...</a>). Can confirm that auto updating golden test assertions does make working with a test suite much more joyful than struggling with each assertion by hand
Recently, I have given up on writing unit tests, instead prompting an LLM to write them for me. I just sit back and keep prompting it until it gets it right. Sometimes it goes a little haywire in our Monorepo, but I don't have to accept its changes.<p>It feels ... strangely empowering.
When I build unit tests around the right routines, I feel like all is right with the world. But some employers consider this gilding the lily.<p>But with LLMs in hand, I can generate entire suites of tests where they're most useful before management has the time to complain. All the little nice-to-have-but-hard-to-google environment tweaks are seconds away for the asking. It's finally cost effective to do things right.
I realize that the example is contrived, but what is the point of writing a test of a fibonacci function if your test harness is designed to just take whatever it tells you and updates the assert to verify that what it told you is indeed what it just told you.<p>This assumes the code you wrote is already correct and giving the correct answer, so why bother writing tests? If, however you accept that you may have got it wrong, figure out the expected outcome through some reliable means (in this case, dig out your old TI-89), get the result and write your test to assert against a known correct value.<p>I wouldn't trust any tests that are written this way.
Oftentimes, the main purpose of writing the tests is to prevent future regressions. This is pretty common for instance in ML (pytorch) code. You put in some tests with various random inputs and assert it against whatever your network says the output is. That way you don't accidentally change how the network works in future refactors.
If you’re a Swift programmer, the swift-snapshot-testing package is a great implementation of these ideas.<p><a href="https://github.com/pointfreeco/swift-snapshot-testing" rel="nofollow">https://github.com/pointfreeco/swift-snapshot-testing</a>
Discussed at the time:<p><i>What if writing tests was a joyful experience?</i> - <a href="https://news.ycombinator.com/item?id=34350749">https://news.ycombinator.com/item?id=34350749</a> - Jan 2023 (122 comments)
> <i>You start writing assert fibonacci(15) == ... and already you’re forced to think. What does fibonacci(15) equal? If you already know, terrific—but what are you meant to do if you don’t?</i><p>Um …duh? Get out a calculator. Consult a reference, etc. Otherwise compute the result, and <i>ensure you've done that correctly</i>, ideally <i>as independent of the code under test as possible.</i> A lot of even mathematical stuff has "test vectors"; e.g., the SHA algorithms.<p>> <i>Here’s how you’d do it with an expect test:</i><p><pre><code> printf "%d" (fibonacci 15);
[%expect {||}]
</code></pre>
> <i>The %expect block starts out blank precisely because you don’t know what to expect. You let the computer figure it out for you. In our setup, you don’t just get a build failure telling you that you want 610 instead of a blank string. You get a diff showing you the exact change you’d need to make to your file to make this test pass; and with a keybinding you can “accept” that diff. The Emacs buffer you’re in will literally be overwritten in place with the new contents:</i><p>…you're kidding me. This is "fix the current state of the function — whether correct <i>or not</i> — as the expected output."<p>Yeah… no kidding that's <i>easier</i>.<p>We gloss over errors — "some things just looked incorrect" — well, but how do you know that any differently than fib(10)?
It is called snapshot testing, very valid technique. Maybe not best suited to a mathematical function like they have here, but I have found it useful for stuff like compilers asserting on the AST, where it would be a pain to write out and assert on the output and may also change shape.
A lot of tests are designed as regression prevention. You know the system is working as designed, but what if somebody comes along and changes the Fibonacci function to compute much more efficiently (and, in the process, makes some arithmetic errors?).
I think “test the function does what it does” is not necessarily the intent here, it’s being able to write tests that fill themselves in and assuming you’ll double check afterwards.<p>That said, I don’t see how it’s much different to TDD (write the test to fail, write the code to pass the test) aside from automating adding the expected test output.<p>So I guess it’s TDD that centres the code, not the test…
> Um …duh? Get out a calculator. Consult a reference, etc. Otherwise compute the result<p>Article:<p>> <i>This is a perfectly lovely test. But think: everything in those describe blocks had to be written by hand. The programmer first had to decide what properties they cared about... then also had to say explicitly what state they expected each field to be in. Then they had to type it all out.</i><p>The article is about not getting out the calculator.
[dead]
I really like this style of testing -- code that can be tested this way is also the most fun kind of code to work with and the most likely to behave predictably.<p>I love determinism and plain old data.
Amazing to see Jane Street uses Emacs. And property-based testing too.<p>> you don’t just get a build failure telling you that you want 610 instead of a blank string<p>So I had to scratch my head a bit because I was thinking: <i>"Wait, the whole point is that you don't know whether what you're testing is correct or not, so how can you rely on that as input to your tests!?"</i>.<p>But even though I didn't understand everything they do yet I do see at least a big case where it makes lots of sense. And it happens to be a case where a <i>lot</i> of people see the benefits of test: before refactoring.<p>> What does fibonacci(15) equal? If you already know, terrific—but what are you meant to do if you don’t?<p>Yeah a common one is reuse a function in the same language which you believe is correct (you probably haven't <i>proven</i> it to be correct). Another typical one is you reuse a similar function from <i>another</i> language (once again, it's probably not been <i>proven</i> it is correct). But if two implementation differ, you know you have an issue.<p>> let d = create_marketdata_processor () in
> ( Do some preprocessing to define the symbol with id=1 as "APPL" )<p>Typo. It's AAPL, not APPL. It's correctly used as AAPL later on.<p>FWIW writing tests better become a joyful experience for we're going to need a <i>lot* of these with all our AI generated code.</i>
> And it happens to be a case where a lot of people see the benefits of test: before refactoring.<p>it's also very nice if you have a test-last working style, that is, develop the code first using some sort of ad hoc testing method, then when you're convinced it's working you add tests both as a final check that the output is what you expect across a lot of different corner cases, and to prevent regressions as you continue development.
This is a cool idea. I wish something like this existed for C#.
In my experience the lack of joy or difficulty with tests is almost always that the test environment is usually different enough from the real environment that you end up needing to kind of stretch your code to fit into the test env instead of actually testing what you are interested in.<p>This doesn't apply to very simple functions but tests on simple functions are the least interesting/ valuable.