TDD, Should We Do It?
TDD (Test Driven Development) is a development cycle that we need to make the test first before the implementation. The cycle is split up into 3 steps, there are:
- Create The Test (RED) : As it’s name, it’s the step to make initial tests before development, mostly we must create a initial empty implementation function and use it on the tests. In this steps, we saw that the test that we created most of them failed
- Implementation (GREEN) : It’s the step to implement all the requirement that we declared in the tests before. In this step, we saw that all of the tests that we created success
- Tidy Up The Code (REFACTOR) : It’s the step to tidy up the code and make the code more efficient. Sometimes we need this step. In this step, we must make a changes that not effect the tests
Another part of TDD that we must keep good is Code Coverage. Code Coverage is a measurement for how far the tests reach out all of the code implementation. We must make a tests that handle all the possibility that will happened in the implementation. Conditional branches and Exception handling are some part of the possibility that the tests must be handled
Testing Limitations
Sometimes you can’t test a code if they have some function that depends to other resource such as REST API call or a package for IaaS (Infrastructure as a Service) such as package boto3 for AWS (Amazon Web Service).
In this case, we need to use Mock
object for making delete_object
abstraction based on our design so the delete_object
function doesn’t to make an API Call to other resources. For further details about Mock
and its features, you can read it in my other article.
Benefits
As we see in the TDD development cycle, we saw that every we make implementation, we have a test that test our implementation. If our application going bigger, automated test is very helpful to keep track on the base logic of the previous version of the app. Sometimes we make a changes to existing codes and the changes make a side effect to another implementation, so if we do TDD, we can watch the error that happened after we make changes and we can fix it immediately. Test in here not just act like trying the code and reported it but also act like error location pointer when failed test occurred
In article that i read, TDD make the application 60% more less bug than not using TDD and also the average debugging time is reduced from ~ 15 hours to 1 hour. It’s a huge improvement for big company to reduce development cost
The test that we created also can be used as a documentation of our codes because it contains snapshots about our codes
In this example, i create a test for function useUserRepoState(),
the criteria describe that when useUserRepoState()
is called it will give an userRepository state output
Another example, i create a test for testing function upload
in package s3, the criteria of the test describe that when upload
called without a key, the function will get the key from the file name and upload it to the s3 using upload_fileobj
function
Besides that, another engineer that saw our tests can easily detect what our codes can do
In this example, the test suite describe my codes can do upload, delete file and get file url in s3
In this example, the test suite describe my codes can handle login, logout action
Disadvantages
From many benefits that TDD delivers to us, it also has some disadvantages.
First, you must have a high experience in the language and tools that you choose and also have a clear information about the requirements. It’s hard to create a test when you don’t understand it.
Second, creating a test is HARD. We need to think all of the scenarios for our applications. We must have big imagination about our implementation later and the program requirements. Your development cycle may stuck because of this.
Third, each language has different test tools. As we saw the examples that have been given, python test tool and JavaScript test tool has different style of code and technique. So we need to learn that first.
Fourth, we make more codes, so it makes development time to be longer than usual.
Conclusion
Implementing a TDD is a good for most application development cases but we must think about several things :
- Our abilities, if you are developing app and learning the tools at the same time, you may consider to create the code first and create the test, after the test is good and you know the pattern, you may run the TDD cycle
- Development time, if you have limited time of development time, you may create the test after the app published
That’s all of my experience in TDD, i hope this information help you all.
References
https://jonathanchristopher1199.medium.com/tdd-perspective-renewed-another-value-c767bdfb9bc5