TDD is just about writing the test before the program. In other words, first think about “how to use” any component, why do we need this component or what’s it for? And only then think “how to implement”. Hence we can say, it’s a testing techniques as well as design techniques.
Below are the advantages of it,
- It results into components that are easy to test.
- It results into components that are easy to enhance and adapt.
- In the end, there is no code without a test.
- We can tell at any time, whether everything still works as it should, or what exactly does no longer work as it once did.
- Complex things won’t fear us, execute the test and get positive feedback.
- You can re-use the unit test in accessing the performance of your system early stage of software lifecycles.
Why test first?
- The test is the executable specification.
- You start thinking about the goal first, then about the possible implementations.
- You understand the program‘s behaviour by looking at the tests. The tests tell you more than just an API description, they show the dynamics, how to use the API.
- You get to the goal as quick as possible.
- You don‘t develop unnecessary code.
- There is no code without a test.
- There is no test without a user requirement.
- Once you get one test working, you know it is working now and forever.
- You use the tests as regression tests.
- The tests give us the courage to refactor.
- You can prove that everything still works after the refactoring by simply executing the tests.
What is unit testing and why do we want it?
A Unit Test is a test of a small functional piece of code. Unit Testing makes developer lives easier.
- Easier to find bugs
- Easier to maintain
- Easier to understand
- Easier to develop
Selecting the Right Framework and Tools
- Choose the framework such a way that using that you should be able to write test cases meeting the below requirement,
- Cover whole your code
- Easy to use
- TDD can be applied to any language and compatible framework should be used. E.g. Junit for Java, nUnit for C# , cppUnit for C++ etc.
- Integrated development environment
- For writing tests, using auto-completion and generation of
- missing code.
- For running the tests
- For refactoring
- g. Eclipse, Visual Studio etc.
- Build environment
- For executing tests automatically and during the build process
- For computing code coverage
- For generating test reports
- g. Maven, Gradle etc.
Good Engineering Practices
- Having a source code control system
- Doing code reviews before checking in code
- Checking in code at least daily (even if it leads to build failures)
- Having daily builds
- Doing unit tests.
- Having an automated test harness for unit tests
- Using test-driven development at the automated test harness level
- Doing continuous builds
- Doing acceptance test driven development
- Apply the acceptance tests once code is checked in