Jakub Zalas Profile picture
May 17 22 tweets 5 min read
On granularity of tests focused on behaviour.

#tdd #bdd #testing #programming

🧵1/22
A function is one of the smallest units of behaviour. Given an input, it returns the output.

This means that given the context (input), the function's behaviour can be verified by calling the function and looking at the outcome (output).

🧵2/22 Pure function
Notice that the most straightforward outcome to verify is the function's return value, but it's not the only outcome we can look for. Functions can have side effects, modify state, interact with collaborators, etc.

🧵3/22
I'm going to talk about functions as a unit of behaviour for simplicity, but most of it applies to objects as well. After all, objects can be thought of as functions with some context encapsulated.

🧵4/22
Functions can be composed to build more complex behaviours out of the simpler ones.

🧵5/22
Composition works all the way up (or down).

🧵6/22
It's a simplification, but an application can be thought of as a composition of behaviours. At the top, there's an entrypoint that the end-user will call. The entrypoint will in turn call lower levels, that will call even lower levels,that will call..., all the way to the bottom.
In my tests, I can choose to verify the behaviour on any level.

🧵8/22
By choosing one of the higher levels, I'm able to verify the behaviour closer to the end user's experience. The cost is less pressure put on the design.

🧵9/22
By choosing one of the lower levels, I'm able to verify the behaviour of components that make up the system. The gain is better feedback on my design.

🧵10/22
The higher level I choose to test through, the more freedom I get in arranging lower levels.

🧵11/22
Since it requires more mental effort to keep myself disciplined and take steps small enough to remain in control, it's not necessarily a good kind of freedom. Furthermore, at a certain altitude, it will be harder to verify behaviours buried at lower levels.

🧵12/22
The lower level I choose to test through, the more organised I am and the smaller steps I take.

🧵13/22
Tests will also give me better feedback on my design while being less business-focused perhaps. I will sometimes need to refactor them or even replace if I decided to change some components or the way they interact.

🧵14/22
So which way do I prefer?

🧵15/22
If I had to choose one, I'd go for the lower, fine-grained level tests.

I tend to write both kinds though, to make sure both external and internal quality is taken care of.

🧵16/22
I like to exercise my acceptance tests through one of the higher levels. I often use them to create a "walking skeleton".

🧵17/22
This kind of tests won't execute every possible path. I won't write a lot of them either. This kind of tests will exercise big chunks of behaviours to confirm and document the business goals. These tests will also make sure everything is correctly wired together.

🧵18/22
These tests merely supplement the tests that exercise the lower levels.

🧵19/22
I like to write a lot more fine-grained level tests. These are smaller, faster to run and write, and more focused. This kind of tests will exercise very small chunks of behaviour.

🧵20/22
These tests will help me to take small steps, keep me disciplined, and give me feedback on the quality of my design.

🧵21/22
🧵22/22

Originally posted on dev.to/jakub_zalas/on…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Jakub Zalas

Jakub Zalas Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @jakub_zalas

May 2
Why do I practice #tdd you ask?

There’s a number of paths I could’ve taken (or combinations of the four).

1) Write no automated tests.
2) Write tests before writing code.
3) Write tests at the same time as code.
4) Write tests after writing code.

🧵 Image
On a project that’s worth practicing #DDDesign, having no tests is not an option. Continuous refactoring towards deeper insights is the foundation of #DDD and it’s too scary to refactor with no tests.
As a result, refactoring won’t happen without tests and code will rot. Over time our code and our understanding of the domain will either diverge, or we’ll stop looking for insights to avoid change.
Read 17 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(