Unit Test in the dev team

Jijie Liu
4 min readOct 4, 2021

Introduction

Recently, I worked on building CI, Continuous Integration, of the frontend for my company, and I tried to bring the units tests to our dev team. It’s a very interesting experience, and I would like to share my thoughts.

This article will talk about the following topics:

  1. What is the unit test? How to write unit tests?
  2. What are the benefits that the unit test can bring to the individual developer and the dev team?
  3. What are the obstacles we will face when we bring the unit test to the team?

Unit test

The Unit test is a way to test an individual unit of code, such as a function, a class, etc. We need to test whether the unit works as we expected.

Usually, it takes 3 steps to write a unit test.

  1. Prepare the data and the dependencies. The data should be the input data and expected data. It's ok to use “fake” data, but it should be close to the real one. The dependencies are the services and the sub-classes that are used. We need to pass the mocked dependencies into the unit test because we should only focus on the unit itself. There are other tests to check whether the units work fine together.
  2. Run the tested codes.
  3. Assert the result with expected data.

Here is a simple example.

The most useful metric of unit tests is the test coverage. It tells us how many codes are tested. It contains 4 parts: statements, branches, functions, and lines. I find the branches report is very useful. It remains the condition I accidentally wrote. What’s more, it points out the uncovered lines, which guide you to write the tests.

the example of test coverage report

Benefits of unit tests

Unit tests bring a lot of benefits to both an individual developer and a dev team. The biggest one is gaining confidence. It helps us understand what we are doing, avoid making stupid mistakes.

For an individual developer, Once the unit test points out the uncovered lines, we will think if the codes are the cases we forgot or these are just useless. When we find it’s hard to mock and test a unit, it tells us the code is over-coupled. We need to split it into smaller and more independent pieces. Without doubts, writing the unit tests helps us sharpen our coding skills.

For the team, the unit test tells us what our teammates have done. It also prevents us from accidentally crashing the team’s work when we add a new feature. It’s for sure that writing the unit test makes our project more robust.

Disadvantages of unit test

There are some downsides to unit tests for a team. It’s expensive!

First, it costs time. Usually, the amount of testing codes is equal to that of the main codes. It may double the time of the development. Sometimes, it costs even more time because the developers need a deeper understanding of the codes. For start-ups or small companies, time is everything. This makes unit tests not necessary for them.

Secondly, it costs money. Either training the current developers to write unit tests or hiring better developers needs more money.

Because of the cost of the unit tests, we shouldn’t bring the unit tests to the team blindly. We can ignore the unit tests of the new feature if the demand is urgent. However, we should refactor the feature with the unit test after, and keep in mind that it’s dangerous to do so.

For individuals, I think there are no disadvantages at all! As we discussed before, the unit test is a good guide. It helps us write as few codes as possible and makes the delivery robust. More importantly, it improves our coding skills and makes us better developers. But be careful, the unit test doesn’t make sure our codes meet the demands. It only makes sure the codes work as expected. Don’t be over-confident because we write the unit tests. We need more testing skills, such as integration tests and end-to-end tests.

Conclusion

Unit test is a great tool for both the individual and the team. It sharpens the developer’s coding skills and improves the project’s robustness. However, it increases the project’s cost and demanded developer more training.

The unit test brings confidence to developers and teams. However, we should keep in mind it’s not a silver bullet. We need to combine it with more advanced tests, and more importantly, keep humble and finding the weak points of the project.

--

--

Jijie Liu

Developer in Paris, interested in building small cool stuffs