Learning Test-Driven Development
By Anthony Mattas
Test-driven development is widely used in enterprise programming, but it wasn’t covered in my computer science classes. I’ve been teaching myself how to use TDD, trying it out in both work and personal projects.
Here’s what I’ve learned so far, along with a few things I’m still unsure about.
TDD is Not “Tested Development”
The main idea is to write the test before writing any code. This is a big change from writing code first and adding tests later. The test defines the behavior you want, and then you write just enough code to make it pass.
It sounds simple, but I found it harder to get used to than I thought.
You Need Stubs First
One thing I never saw in TDD tutorials is that you can’t write a test for a method that doesn’t exist yet. You need at least a stub, which is just an empty method signature, so your test will compile.
Looking back, it seems obvious, but it confused me at first. The real workflow is to create a stub, write the test, and then implement the method.
Mocking Gets Complicated Fast
After simple tests, you need to use mock objects. There are lots of mocking frameworks out there, and I’m still looking for the best one. If you have any suggestions for mocking libraries, I’d love to hear them.
The 100% Coverage Question
Should you write tests for getters and setters? If you want 100% coverage, then yes. Still, testing something as simple as return this.foo can feel pointless.
On the other hand, getters and setters can get more complex over time. If you add validation or lazy loading, you might be glad you wrote those tests. I’m still figuring out where to set the boundary.
A Tangent on Abstraction
I graduated as a dedicated C programmer, and I still like using C. But now that I use higher-level languages every day, I’m surprised by how much more I can accomplish.
Still, I wonder about new developers who might never deal with pointer arithmetic or manual memory management. Those challenges taught me how computers really work. Do we lose something when everything is abstracted?
Category: Development
Comments
Sign in to join the conversation
No comments yet. Be the first to share your thoughts!