r/functionalprogramming • u/ganjaptics • Feb 24 '23
Question Is there a functional approach to writing tests?
I try to write functional code as much as possible, even in not so functional programming languages. However, I've noticed that my tests tend to be very imperative, no matter what language. This is especially true for the higher level tests I write (like integration and e2e tests.) So, is there any theory or literature on writing functional tests? Specific monads or patterns?
I'm mostly concerned with testing web applications.
21
Upvotes
19
u/josephjnk Feb 24 '23
Look into property-based testing with quickcheck, smallcheck, or hedgehog. There are implementations for one or more of them in just about every major language. They focus on specifying the high-level semantics of code’s behavior in a more precise way. I don’t get to use it much but I’ve really enjoyed the fast-check library for TypeScript in the past.
For an example of it being used at high power, check out this Benjamin Pierce talk: https://youtu.be/Y2jQe8DFzUM
7
u/crdrost Feb 24 '23
Just wanted to mention the Haxl talks, turns out you can abstract all I/O as messages and responses, Erlang-style, and then you can cache responses so that you don't rerun the same queries twice. This gives a very strange way of doing tests, which is to put the code under test into a harness which frontloads the cache, mocking everything declaratively.
This points to the central tension of tests, why they struggle to protect us. (Rich Hickey put it this way, “what two things do you know about every bug in production? It passed the type checker! And it passed the tests.”) A test lives in a non-deterministic language, especially if the code under test is not deterministic, but even if it is, most languages mutable variables are default, and so forth. But flaky tests are the worst! They are so bad!!
So this may point to your tension around integration and end-to-end testing, perhaps the imperative style is part and parcel of trying to limit nondeterminism, set up exactly this scenario and then push that button and see if exactly this scenario happens, then push this other button, and so forth. If so then maybe the problems are irreducible: any integration test that can be decoupled into deterministic cases becomes some assembly of contract and unit tests. Call it the CUT-conjecture and give me some credit if you publish it, hah.
7
u/DogeGode Feb 24 '23
I've noticed that my tests tend to be very functional, no matter what language. Easy enough:
However, I'll admit that most of my tests are not integration or E2E tests.
6
u/tweek-in-a-box Feb 24 '23
If you're using Free Monads to model your business logic one approach is to have a test interpreter so you at least have pure tests for your business logic. Then all that's left to integration test is the actual implementations behind your production interpreter (like e.g. some HTTP client talking to some deps). Something like this for example: https://www.tiny.cloud/blog/tiny-cloud-free-monads/
3
u/redchomper Feb 24 '23
Preamble: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD03xx/EWD303.html
Tongue-in-cheek response: No, there is no approach to writing tests that functions.
Serious answer: The data-type of "test" is something like
sut -> stimulus -> response -> booleanwhere the test framework deals out a summary report on thosebooleanresults. Emitting the report is a side effect, but constructing it does not have to involve them. However, supposing the system-under-test requires effects to construct, then you'll find a sense of procedure skulking in. (effect-algebras notwithstanding)3
u/[deleted] Feb 24 '23
When I write programs (even js web apps) there are two parts: the functional core and the imperative shell. The part with the immutable data and pure functions, the core, is as good as you can get for writing functional tests. It's all computation and no side effects.
There's nothing special which needs done. It's only when you're mixing imperative and functional code that this becomes confusing.
4
u/XDracam Feb 24 '23
The more static checking you have, the fewer runtime tests you need. That's why there's a rather small focus on test code in purely functional languages.
Ironically one of the biggest Haskell testing frameworks is HUnit, a library directly inspired by java's JUnit, like so many others.