TDD: Test Driven Development

More like, Test Driven What are we making again?

Muhammad Naufal Irbahhana
HappyFresh Fleet Tracker
3 min readOct 31, 2019

--

source: “Google Image”

TDD is the acronym for Test Driven Development, it relies on the repetition of a very short development cycle. The development first carried out by a test of a specific requirement and then after implementation we can refactor our code as in adding our improvement to it. So, what is TDD again?

Definition

Instead of writing functional code first and then your testing code as an afterthought, if you write it at all, you instead write your test code before your functional code. Furthermore, you do so in very small steps — one test and a small bit of corresponding functional code at a time. Taking a TDD approach refuses to write a new function until there is first a test that fails because that function isn’t present.

Phases

There are three phases of TDD. Namely [RED],[GREEN],[REFACTOR]. So each phases would be:

RED

Red basically thinking about what you want to develop. The purpose is to write a test that informs the implementation of the features. It has to be specific, so that it can be fulfilled by the implementation of the actual code. it basically define what you want to implement

GREEN

The green phase is when you think about how to make the test pass. You basically implement the code. Basically it is to find the solution without worrying about optimizing the code. it mimics the green checklist of a passed test in unit testing. so it is called the Green phase

REFACTOR

Refactor is the final phase of TDD. Here you have to think on how to implement your code efficiently. You need to think on how to accomplish the same output with more descriptive or fast code. You may not have to refactor you code, if it’s already efficient enough and usable, you can just do another repetition of the TDD cycle starting from red again.

Coverage

Code coverage is a measurement on how many tested line/blocks/arcs of code that is tested and executed. Usually, code coverage is measured by percentage. It really is varies between each project. Usually it’s above 80%.

There are several code coverage criteria such as paths, conditions functions, statements etc. Usually the most common thing to be covered is:

  1. Condition Coverage:all boolean expression has to be evaluated true or false.
  2. Decision Coverage: all subsequent if else body also evaluated
  3. Loop Coverage: means, has every possible loop been executed in one time, more than once and zero time.
  4. Entry and Exit Coverage: Test for all possible function call and it’s return value.
  5. Parameter Value Coverage: Test if all parameter value on a function is tested.
  6. Inheritance Coverage: If you have an OOP structure code, the child of the class should return the same on what the parents are returning.

TDD In Our Project

UnitTest Example

Handler for websocket test

Here i only display the test function. For the setup you can refer to our gitlab. We can see that there are two tests available. First the websocket test with nil connection and test with mock data. First, the function Convey is to specify on what you are expecting on the test. Next So function is to assert whether the output is as expected. In this test, we assert as ShouldNotBeNil . h.Connect execute the function so that it is asserted. and we expect that err variable is not nil. the byte of array is what the websocket protocol upgrader expect.

Benefit

Mainly, TDD can benefit you in several cases, such as

  1. Easy anticipation and identification of bugs
  2. Tidier Code
  3. Fewer Bugs
  4. Safer Refactoring
  5. Living documentation

Conclusion

TDD is one of the best way on software development cycle, it has been proven to be helpful among developers. So using TDD can be beneficial or also can turn sideways because of time constraint or etc.

Thank you for reading

--

--