XenonStack

A Stack Innovator

Post Top Ad

Thursday 5 December 2019

TDD, Test and Behaviour Driven Development and Unit Testing in Python

Overview

Test Driven Development (TDD) is a great approach for software development. TDD is nothing but the development of tests before adding a feature in code.
This approach is based on the principle that we should write small codes rather than writing long codes. In TDD, whenever we want to add more functionality in our codes, we first have to write a test for that. After that, we add new functionality with small code lines combined and then test it with our test. This approach helps us to reduce the risk of encountering significant problems at the production level. You may also love to read more about TDD and Unit Testing in this blog.

Test Driven Development (TDD)

Test Driven Development is an approach in which we build a test first, then fail the test and finally refactor our code to pass the test.

Test Driven Development (TDD) Approach

As the name suggests, we should first add the test before adding the functionality in our code. Now our target is to make the test pass by adding new code to our program. So we refactor our code to pass the written test. This uses the following process –
  • Write a failing unit test
  • Make the unit test pass
  • Repeat

Test Driven Development (TDD) Process Cycle

Test Driven Development Process Cycle
As shown in the flow
  • First, add tests for the functionality.
  • Next, we run our test to fail.
  • Next, we write code according to the error we received.
  • Then we run the tests again to see if the test fails or passes.
  • Then refactor the code and follow the process again.

Benefits of Test Driven Development (TDD)

Now the question arises why should one opt TDD approach. Practicing TDD brings lots of benefits. Some of the benefits are listed below –
  • In TDD  we  build test before adding any new feature to it, that means in TDD approach our entire code is covered under the test. That’s a great benefit of TDD as compared to the code which has no test coverage.
  • In TDD one should have a specific target before adding new functionality. This means before adding any new functionality one should be clear about its outcome.
  • In an application, one method depends on the other. When we write tests before the method that means we should have clear thoughts about the interfaces between the methods. That allows us to integrate our method with the entire application efficiently and help in making our application modular too.
  • As the entire code is covered by the test that means our final application will be less buggy. This is a big advantage of the TDD approach.

Acceptance Test Driven Development (ATDD)

ATDD is short for Acceptance Test Driven Development. In this process, a user, business manager and developer, all are involved.
First, they discuss what the user wants in his product; then business manager creates sprint stories for the developer. After that, the developer writes tests before starting the projects and then starts coding for their product.
Every product/software is divided into small modules, so the developer codes for the very first module then test it and sees it getting failed. If the test passes and the code are working as per user requirements, it is moved to the next user story; otherwise, some changes are made in coding or program to make the test pass.
This process is called Acceptance Test Driven Development.

Continue Reading atXenonStack/Blog




No comments:

Post a Comment