2020-05-14

TDD for beginners

Raman

ラマンと申します。 私の出身はインドで2年前に日本に来ました。 日本語があまり得意ではありませんので、英語で書かせてもらいます。

What is TDD?

  • Test Driven Development is a process of developing and running automated test before actual development of the application.
  • Sometimes TDD also called as Test First Development.
  • Technically, TDD is used to implement the function by using test cases.

What are the steps?

Step - 1 :: Write all test codes which you can think in the starting that fails based on the requirements of the actual function . Step - 2 :: Modify the code and test it again . Step - 3 :: Once each test passes then do refactoring . Step - 4 :: Repeat step 1 to step 3 until all test are passed . Step - 5 :: You can add test later if you did not think in the starting and repeat Step 1 to Step 3 .

Graphical represntation

TDD

This representation is divided into three zones. Red :: In this zone, developer will write the code then test will fail. Green:: In this zone, developer modify the code and test will pass. Blue:: In this zone, developer will refactor the code.

Is TDD has any dependency ?

No, TDD is independent of any programming language. This means, you can use in php, javascript, go and other programming language.

Let's learn with an example

Check the length of the string. The length of the string should be between 6 and 10 .

  • I am using golang to explain this task. You can choose your own favorite programming language.

Step 1

  • Create a main logic file length.go and write the logic which will fail .

StringLength - red-zone

   package length
   
   // Check String Length should be between 6 and 10
   func CheckStringLength(str string) bool {
     return false
   }
  • Create test file length_test.go which will use logic from length.go TDD test file
package length

import (
    "testing"
)

func TestCheckStringLength(t *testing.T) {
    var isValid bool
    isValid = CheckStringLength("test123")
    if isValid == false {
        t.Error("The string length is incorrect")
    }
}
  • Test result Test result

Step 2

  • Modify the code in main logic length.go TDD step 2
package length

// Check String Length should be between 6 and 10
func CheckStringLength(str string) bool {
    isValidStringLength := false
    if len(str) > 5  {
        isValidStringLength = true
    }
    if len(str) > 10 {
        isValidStringLength = false
    }
    return isValidStringLength
}
  • Test the code TDD step 2 test result

Step 3

  • Refactor the code in main logic length.go TDD step 3
package length

// Check String Length should be between 6 and 10
func CheckStringLength(str string) bool {
    if len(str) > 5 && len(str) < 11 {
        return true
    }
    return false
}
  • Test the code TDD step 2 test result

Like this, if you want to add or missed any test case then add the function and test the code after adding test case. This will verify that the previous added function works well irrespective how many test case you will add.

Why should be used in development?

  • Early bug notification.
  • Better designed, cleaner and more extensible code.
  • Confidence to refactor.
  • Good team work.
  • Good for developers.

Conculsion

  • TDD stands for Test Driven Development.
  • It focuses on production code rather than test case design.
  • It is a process of modifying the code in order to pass a test previously.
  • Changing/adding some amount of code to the existing code without affecting the behavior of the code.
  • The code becomes cleaner and simple to understand.
  • TDD is independent of any programming language.

References

https://en.wikipedia.org/wiki/Test-driven_development https://www.valtes.co.jp/qbookplus/1069 https://golang.org/pkg/testing/

最新の記事