• info@helpingtesters.com
  • helpingtesters

Test Driven Development (TDD)

August 31, 2017
test driver development, TDD, BDD, test driver development example

Let us understand firstly why Test Driven Development is required? When you start building an application, you have to make many choices even before you write the first line of code. You can choose to build the program cheaply and quickly expecting it to work down the line. A good program not only works, but it is also easily comprehensible, modular and easy to maintain. A well-written code base not just saves time and money in the long run, it can withstand the tests of time with changing business requirements. Initially, teams might feel that it is adding unnecessary effort, but it facilitates easier ongoing delivery. To understand code written by another person is always a difficult task, especially if his style is different from yours. Just like different people write a language differently, different coders write codes in their unique style, but not all styles are equally good.

Many programmers don’t even follow basic coding practices which include proper commenting, good indentation, coding vertically, etc. For large projects, there is a style guide which every programmer must follow. Test Driven Development solves all these issues by implementing refactoring measures, making coders follow best coding practices and constant testing.

What Is Test Driven Development

  • Test Driven Development is a software development methodology in which you write a test before you write code. The test acts as a specification which ensures that the code does what it is supposed to do.
  • Test Driven Development uses unit tests to drive the development of the software. The basic idea behind the unit tests is to write tests that exercise the smallest unit of code possible. Unit tests are written in the same programming language as the source code of the program. A unit test is a code that tests other codes.
  • Besides Unit Testing, another aspect which is extremely important in Test Driven Developmentis refactoring. Refactoring brings different programmers on the same page. It improves the internal structure of the codebase without affecting the external behavior. It improves code readability, supports reusable design elements and implements best coding practices.
  • All the coding is interwoven with refactoring and unit testing in Test Driven Development. Test Driven Developmentimplements an agile software development process for clean, simple design and code.

Now that we are well aware of the basics of Test Driven Development, let’s refer a flowchart and take a practical example to understand this development technique when implemented in a project.

A typical Test Driven Development flow

When the requirement (or feature) of a login page arrives, right after requirement grooming, developers would start coding the feature, while the automation team would start writing test scripts to automate the login page. This is the ideology of Test Driven Development.

The tests (even unit tests) could be basic tests that would cover basic feature with an intend to failure. When the script fails, the team would know that the feature is not performing as intended and that some more work needs to be done. Then when the development team actually finishes the feature and check in the code, the automation testers would make suitable changes to the scripts. Now, the test case would pass indicating that the feature has been developed properly while sticking to the guidelines. Now the development team can move on to other features. Thus the complete feature delivery is guided by tests rather than the source code.

Once the test case specific to the feature passes, new test scripts should be added and run (which would fail). And the process would continue.

Different Tasks Performed In Projects With Test Driven Development Implementation

⇒ Focus On The Purpose

You must focus on what you need to achieve by the end of the development process. Long-term development needs to be kept in mind. Functionality keeps getting added to the software. Future developers must be able to use the test and understand the intended functionality.

  • Test cases must follow the standard naming conventions and elucidate the intended purpose. Make sure that you understand where the code needs to be recalled while framing the test suite.

⇒ Functional Complexity

The test case drives the development, and it must be reviewed for completeness and correctness.

⇒ Spelling Out The Rules

Once the standards are set, organizing tests in a logical pattern become easier. It is important to ensure that everyone in the team understands the conventions that need to be followed.

  • Implementation of popular conventions helps the new members to leverage the existing knowledge and increase their productivity.

⇒ Code Austerity

Make sure that your code is minimalistic and does not add any clutter to the codebase. It simplifies the review process and minimizes errors.

  • Apart from precautionary measures, developers, as well as testers, must participate in code refactoring, to improve the code quality and check redundant scripts and functions.

⇒ Road-Map Of Tests – Refactoring plays a major role in Test Driven Development. The test-first approach enables you to sketch the thoughts and break it down into cases. It is recommended to sketch a roadmap of tests for a better workflow.

⇒ Constant Testing – You must test the code repeatedly before coding and after coding. Test again after refactoring. Ensure the correctness, completeness and good standard of code at every step.

  • The focus should be put on continuous testing and test scheduling so that the test cases can be run daily without fail.

⇒ Code Sanctity – Use version control software to maintain code sanctity. When more than one developer works on the code, integration tools can help avoid merger issues and other discrepancies.

  • Even gatekeepers can be assigned who would look at every merge requests to ensure that the quality of the test code base is not compromised. If any hint of the improper coding standard is found, the merge request should be rejected and the respective automation tester should follow up with the gatekeeper to improve the code quality.

⇒ Proper Application Knowledge – Coding needs to be limited, and it must achieve the purpose without creating any new problems. It is important to maintain adequate documentations and repositories of tests, Test Driven Development works best with developments that can be tested quickly. Prolonged or complex testing often defeats the purpose of Test Driven Development.

 

Benefits of Test Driven Development

  • Speed and Quality – Initially, it may feel like unnecessary work, but down the line, it results in a clear understanding of the software, accurate estimates, superior code quality and streamlining the development process.
    • Developers get a guideline for coding which results in producing high-quality code that follows good standards and which is always testable.
  • Measurement Scale – Running tests during the development process help to get the feedback about the code quality and the bug rates.
  • Tidy Codebase – The tidier code allows demarcating a clear line between the private and public methods. It is easy to keep your classes private if you implement Test Driven Development right from the beginning.
  • Fewer Bugs – With the constant testing, you minimize the bugs in your codebase and save time that would be spent in fixing those bugs later.
  • Productivity – Following best coding practices, reducing merger issues and adopting a common style guide increases the overall productivity of your team.
  • Return On Investment – Test Driven Development costs high in the beginning, but it can reduce a lot of resources down the line. As the code base starts getting bigger and development becomes more complex, TDD implementation saves a significant amount of resources.

 

Test Driven Development Practices

Every team member actively participates in the implementation of Test Driven Development while working on the project. Programmers with different skill levels have different responsibilities towards it.

⇒ Beginner – A beginner must be able to write a unit test before writing the code.

  • He must be able to write sufficient code to pass the test.

⇒ Intermediary – An intermediate developer must be able to write a test to expose a defect.

  • He must be able to guide the beginners to write test codes.
  • He should be capable of breaking down a complex program into a series of unit tests.

⇒ Experienced – Advanced developers should be able to write the road maps and revise them as necessary.

  • They must be able to run advanced tests for technical domains and design paradigms.

 

Common Hurdles in Test Driven Development

  • Slow Development – Writing tests for every line of code of program consumes a lot of time, and it can severely impact the speed of development.
  • Writing Test Cases – Sometimes, writing tests for a specific development is not easy and impacts productivity.
  • Continuous Changes – It becomes hard to test constantly evolving interface and the rapidly changing design.

Conclusion

  • Test Driven Development is advantageous for long-term development. Initially, it may add extra costs to the project, but in the long run, it saves a significant amount of resources on fixing bugs and errors.
  • It offers a lot of benefits such as clean code base and easy collaboration, but it has some challenges associated with it such as slower development process and difficult implementation.

Blog is credited to HelpingTesters.com team

About the author

HelpingTestersTeam administrator

Blog is credited to HelpingTesters.com team

Leave a Reply

Your email address will not be published.