Test Driven Development

Nicolaus Christian Gozali
Moodah POS
Published in
3 min readNov 14, 2019

What it is and how we do it in Moodah POS

src: https://usersnap.com/blog/web-application-testing/

TDD is an approach where developers will write tests first before any functional code, then implement features just enough to pass said tests. This may seem weird at first, but its discipline is very useful in the long run.

In my own experiences in Moodah POS, a point of sale section that is part of a larger ERP application, I will share our process in applying TDD.

Definition

TDD is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases that will fail, then the software is improved so that the tests pass just passes and do code refactoring if needed. This is continuously repeated until all features are present.

src: https://www.allaboutcircuits.com/technical-articles/how-test-driven-development-can-help-you-write-better-unit-tests/

Benefits

Applying TDD for those new to the concept may seem counter intuitive as much time is spent thinking of test cases ahead of implementation. However by doing so, the following benefits may be reaped:

High code and logic coverage

By writing code that barely passes the test on every cycle, we ensure that every added logic is well tested. This brings to the next point.

Easily detect unintentional side effects

When the app grows in complexity, there might be a time where old code needs to be modified or interacted with. With TDD, any errors can be caught quickly by the failing of old tests.

Higher quality code

There will be fewer lurking bugs hiding and most will be detected as soon as it is created due to earlier point. This creates cleaner code that is robust and the team can guarantee that at any point, code is working well as long as it passes all tests.

Next, we will take a look at some test we implemented.

Unit Test

This level of software testing concerns individual components of a software to be tested. The purpose is to validate that each unit of the software performs as designed. A unit is the smallest testable part of any software. Here, we mocked response from Odoo to test implementation of our GraphQL middle ware resolver. Mocking in short refers to replacing actual implementation or network calls with configured response.

unit test of fetch all query with help of mock

Above test uses Jest mocking features to not actually actual requests to Odoo, this way we can isolate our tests to only our GraphQL resolver.

Integration Test

This level of software testing is where individual units are combined and tested as a group. Here, we combined our GraphQL middle ware and Odoo back end with purpose of exposing faults in the interaction between them.

a test of paymentMethods query

Coverage

Code coverage is a common metric used to identify how well is the code tested and covered by the tests. It tells us the percentage of number of lines touched by all tests. This metric is not a great metric to truly identify quality of test cases, but at the very least code coverage should be high.

A common practice it to keep this at a high percentage at all times, below is our coverage for back end.

recent pipeline to staging

That is all regarding TDD and its benefits in our project. Thank you for reading, cheers~

--

--