Boring Tests
Testing feels boring when code makes it a slog. Pure functions, exhaustive unions, FsCheck properties and Testcontainers make it a fast safety net.
The Codewali posted about how test writing is boring.

I've certainly felt that way before, and I thought it was worth chiming in.
Here is an expanded response to what she said, with some more examples:
I think it really depends on how you do it.
It CAN be boring, but I think a lot of that is a result of how you write your code. Building software in a typical OOP way makes testing a hellacious nightmare of mocking and grunt work all to build enough scaffolding around the problem at hand so that you can assert one or two little things, and it tends to be quite a slog because it's difficult to resist the feeling that you're wasting your time and not gaining much value. Boring comes from a task having high effort and low return on that investment, and it's why many can't stomach doing any.

However, especially in enterprise software, we do need some kind of safety net to assure our systems are behaving correctly, other than manually having some poor soul, whether ourselves or the QA engineer cursed with babysitting our changes, go through each little user interaction flow manually and follow some cursed written checklist of steps like a caveman.
Never send a human to do what a computer could do a hundred times a second.

We need to take approaches that prove the behavior of the system is turning out as expected without wasting human time. The fact of the matter is that good testing is orders of magnitude faster than having a manual QA process for what we can think of beforehand.
So how are we supposed to do it without wanting to gouge our eyes out?
I take a few approaches:
The first and most important one is to make your code lend itself to being properly tested. There is nothing easier to test than pure, simple, static functions doing computation with immutable data. It doesn't get any more straightforward than that. You give dumb data to a transformation and get expected output. That's it. I've said it once, I've said it a thousand times, complexity is the enemy.
Global mutable state that is hard to set up and keep track of will always be the death of testable software. We need to have enough discipline to avoid the trap of complecting behavior and data. The ideals and the defaults of functional programming exist as the antidote to that exact kind of chaos. If we set up our domain to make illegal states unrepresentable we can leverage that to enforce at compile time what we would have to hope and pray behaves correctly at runtime.
By using Discriminated Unions/algebraic data types to model our behavior and transformations we lean into exhaustive pattern matching, and it means the structure of what is even possible to express becomes a compiler backed guarantee.
type CreditCardInfo = { CardNumber : string; ExpirationDate : string; NameOnCard : string; SecurityCode : string }
type PayPalInfo = { Email: string }
type BankTransferInfo ={ RoutingNumber :string; AccountNumber :string}
type PaymentMethodType = | CreditCard of CreditCardInfo | PayPal of PayPalInfo | BankTransfer of BankTransferInfo | Cash
let processPayment paymentMethod =
match paymentMethod with
| CreditCard c -> printfn "Credit card payment received %s %s %s %s" c.CardNumber c.ExpirationDate c.NameOnCard c.SecurityCode
| PayPal p -> printfn "PayPal payment received %s" p.Email
| BankTransfer b-> printfn "Bank transfer payment received %s %s" b.RoutingNumber b.AccountNumber
| Cash -> printfn "Cash payment received"
CreditCard { CardNumber="1234-4321-1234-1234"; ExpirationDate="12/2029"; NameOnCard="John Doe"; SecurityCode="123"} |> processPayment
PayPal {Email="[email protected]"} |> processPayment
BankTransfer {RoutingNumber="000000000000"; AccountNumber="00000000000000000000"} |> processPayment
Cash |> processPaymentThen we can go further by using property based testing, like with the incredible Expecto Library with FsCheck for dotnet, to have our functions battle hardened by as many random and antagonistic inputs as possible, and have it automatically shrink to the ranges where your assumptions about how the function is supposed to be working break down in ways we can see easily.
module Expecto.FsCheckTests
open Expecto
open Expecto.Impl
let properties =
testList "FsCheck" [
testProperty "Addition is commutative" <|
fun a b ->
a + b = b + a
testProperty "Deliberately failing test" <|
fun a b c ->
// wrong on purpose to test failures
a * (b + c) = a * a + a * c
testProperty "ignored" <| fun _ ->
skiptest "Because I feel like it."
ptestProperty "ignored2" <| ignore
]
[<Tests>]
let runFsCheckTests =
testCaseAsync "run" <| async {
let! results = Impl.evalTestsSilent properties
Expect.equal results.Length 4 "results length"
let getResult name =
results
|> Seq.filter (fun (r,_) -> r.name=name)
|> Seq.head
|> snd
Expect.equal (getResult ["FsCheck";"Addition is commutative"]).result
TestResult.Passed "passed count"
match (getResult ["FsCheck";"Deliberately failing test"]).result with
| TestResult.Failed _ ->
()
| x ->
failtestf "Expected Failed, actual %A" x
match (getResult ["FsCheck";"ignored"]).result with
| TestResult.Ignored e ->
Expect.equal "Because I feel like it." e "It should fail with the right message."
| x ->
failtestf "Expected Ignored, actual %A" x
match (getResult ["FsCheck";"ignored2"]).result with
| TestResult.Ignored _ ->
()
| x ->
failtestf "Expected Ignored, actual %A" x
}
let focused =
testList "FsCheck focused" [
testCase "ignore me" <| ignore
etestProperty (1UL,3UL,Some 50) "Deliberately failing test" <|
fun a b c ->
// wrong on purpose to test failures
a * (b + c) = a * a + a * c
]@ScottWlaschin taught about how powerful this testing can be in his article on the fantastic fsharpforfunandprofit.com article series called the Enterprise Developer From Hell. It's genius stuff and regardless of what language you use it's required reading in my mind.
On a broader testing note, I think anyone not using @testcontainers is just simply leaving a goldmine on the table. You want your tests to run against real infrastructure. A real database. A real message queue. Not a stupid mock. Not an in memory efcore store that doesn't even run the exact SQL your prod would be running. Real proof of end to end interactions. Real http calls. Real signals in the noise.
They've got stuff for every major language, we have to use it.
I plan on writing another blog post about that, I'll link it when it drops, because that's a whole other subject and there are very few good examples of them being used directly in F#, so I'm going to expound further on it, give some strategies for setting it up that have worked for me, etc.
And then lastly things like snapshot testing and mutation testing are really awesome. Snapshots are great for having readable human output you can confirm looks good is fantastic. I use Verify.
Here are some examples from their github:
module Tests
// begin-snippet: SampleTestExpecto
open Expecto
open VerifyTests
open VerifyExpecto
[<Tests>]
let tests =
testTask "findPerson" {
let person = ClassBeingTested.FindPerson()
do! Verifier.Verify("findPerson", person).ToTask()
}
// end-snippet
// begin-snippet: UniqueForSampleExpecto
[<Tests>]
let uniqueTests =
testTask "unique" {
let settings = VerifySettings()
settings.UniqueForRuntime()
do! Verifier.Verify("unique", "value", settings).ToTask()
}
// end-snippet
[<Tests>]
let typeNameTests =
testTask "typeNameTests" {
let settings = VerifySettings()
settings.UseTypeName("CustomTypeName")
do! Verifier.Verify("typeNameTests", "Value", settings).ToTask()
}
[<Tests>]
let methodNameTests =
testTask "methodNameTests" {
let settings = VerifySettings()
settings.UseMethodName("CustomMethodName")
do! Verifier.Verify("methodNameTests", "Value", settings).ToTask()
}Mutation testing is a watchdog, it makes sure that if you mess with the tests and start flipping their assumptions they break like they should.
Over time we start to wonder if the tests are passing because they're actually telling us something useful, or if it's because they're unable to fail.
A good mutation testing setup helps with giving you more confidence about that.
I'm still working on finding good solutions for this in F#, as the mutation testing landscape is a little dry from what I've seen, but if you're doing C#, you can certainly make use of Stryker.NET. I'd be remiss if I didn't also make mention of Mark Seemann and his site, blog.ploeh.dk which has tons of great articles about testing like this one: Critiquing tests.
Testing doesn't have to suck.
We just have to embrace different mindsets to make simple, maintainable, composable code that lends itself to being tested.
Then it can actually be fun sometimes.

Link to the original X discussion: https://x.com/WillEhrendreich/status/2091900546575712421