Test Driven Development (TDD)
TDD allows for faster development. Allow me to explain.
Any functionality that you write code for either has dependencies or does not. But every functionality has, well, a function to perform. What this means is that every test we write will validate whether or not the function does what we intend it to do, and only some tests require us to do something about those dependencies. The dependencies typically dictate what the function can do.
Lets break this down into pieces:
- A function without parameters and without dependencies.
- A function without parameters and with dependencies.
- A function with parameters and without dependencies.
- A function with parameters and with dependencies.
That’s it. Those are the four scenarios you will ever test. We will not do a deep dive into these, as that’s beyond the scope of this article. What’s important to know, is that tests for functions with dependencies are slightly more complicated than those without, but they are not overwhelming. They require you to think about how your function will behave, say, if a database does return the records you expected, and how will it behave if it doesn’t. If your function has too many dependencies and your test will be a monstrous test, then the function will likely need to be factored out. This is a good example of how testing first helps you think about optimal resuable code early in the process.
Unit tests help us code faster, more reliably, and ensure resiliency. Test Driven Development (TDD) is a discipline, as such, if we are not disciplined we will not succeed at it. I would say, put $1 in a jar for every function that’s not written using TDD. But that’s not really fair, we already have enough to worry about by trying to refactor and making something do what we thought it would do, but it did not. Why write tests, and why write them first.
Get Yousef’s stories in your inbox
Join Medium for free to get updates from this writer.
We write tests because it ensures that contracts among services are not broken without proper planning, they help developers walk into projects with more confidence, they allow us to find bugs and fix them faster, and make adding new features a breeze. Imagine calling an architect over to inspect a 100 story building that is tilting without blueprints or specifications that define what this building is? Maybe its made out of cardboard. Well, that’s what unit tests are. They are the specifications and blueprints of your application. When a bug manifests itself in production, can you easily replicate it on your development environment via a unit test, make the test fail in the same way that the production application failed, and implement functionality that covers that failed scenario, thus making the test pass and ultimately the production application behave as expected? If your answer is no, then you are the architect walking into the building without blueprints. What architect builds a building that way? None. As an engineer, neither should you write applications without testing.
We do test first, because it helps us think about what we want our code to do. It helps us think of reusable functions thus, thus writing less code. If your functionality depends on a different functionality being developed, you are not blocked, you can mock the dependency and finish your feature and ensure it works based on cross-team specifications. It helps us think of the inputs and outputs, including complicated inputs, simple inputs, and invalid inputs. Testing these scenarios ensure resciliency. When you first write your tests, they will fail because there is no actual functionality doing what you are testing. When you write the implementation, the test should pass. What this means is that your code works, and you can move on to the next thing. So, you know you are writing working code, you are not hoping your code works and spend extra time fixing it when it does not.
If you do not do test first, you probably are not thinking in a methodical way about what you want your code to do. Your code will likely suffer of lack of generics, you will have repeatable code, your code will not work, and you will eventually need to go back and refactor and think methodically. You will also likely not write tests, and if you do, they will be bad tests because they are tests written to satisfy code not thought through very well — that’s something that will reveal itself when multiple developers end up writing the same thing in multiple projects, or within the same project.
In order to get to quality working code, you will end up eventually thinking methodically through several layers and steps, when you could have done it in one step, TDD. Finally, if you do not write tests at all, you will end up spending the time testing your code one way or another. You will spend three hours deploying, smoke testing, fixing issues, deploying again, and repeating the process over and over again until it “works.” I say works in quotes, because its ephemeral, it will work until it stops working without you knowing about it. This is not a good approach, the time spent testing in this manner is time other developers will need to spend when they go back to this project to fix what broke, they did not benefit from the time you spent. Why not spend the time writing the unit tests and have the deployment process ensure tests pass and that the code is production-ready, fresh, tested, and working code to the team’s expectations?
Lets recap, how does TDD save us time and help us developer faster:
- You think methodically at the outset, without needing to go back and rethink your code multiple times.
- You are not blocked by dependent functionalities, you can finish ahead.
- Developers can find and fix bugs faster because bugs are easy to replicate.
- Developers can add new features with ease. They can just write a new test that fails, then implement it and make it pass. If older tests do not break, they have succeeded. And if they do break, they will know, and they will fix it right there and then before they break production.
- The functionality you write today and spend X hours on is passed on to other developers. They don’t have to spend X hours again on it.
- You have reusable code, that means that you wrote less code.
In order to ensure robustness, fast development, and good customer reputation, tests go beyond only unit tests. Integration tests ensure that the dependencies “faked” in unit tests continue to respect the contracts, and if they ever don’t for any reason, your team knows about it. Discovery services can help monitor and alert on your services up time, and performance. Regression testing can help ensure your product continues to support legacy system until you pull the plug. Stress testing helps find your system’s breaking point and inform the scaling criteria and further algorithm optimization requirements. And usability and acceptance testing ensure your customers like your product. What good is it to have a tested working product that no one other than you wants to use?
I hope you are motivated to try test first. Happy coding!







