It’s Unit Testing time!!!
I am having an interest in ways of writing better unit tests. I tried different methods and different approaches. I thought of writing this from my recent experience with unit testing. I personally think as developers we need to provide better care for unit tests.
In the end, that is the validator of what you have written and that is someone step up for you when the code is broken. Yeah!!! unit test is like a true love 😅🥰
Now back to topic!
When I am looking for the code written by someone else I always check for the unit test to get a better idea on the functional aspect of the code. There are two main things we can do to make the unit test is more explanation in a functional aspect.
- Write unit tests for actual functional behaviours
- Use real-like values for the data creations in unit tests
Write unit tests for actual functional behaviours
Writing unit tests considering actual functional behaviours would be more beneficial and will keep the developer and the code in a safe place at the production. having 100% coverage doesn’t mean that you have covered all functional paths. But covering all function paths will bring you a code with 100% coverage.
In this post, I am mainly considering the second point.
Use real-like values for the data creation in unit tests
Most of the cases in unit tests we create the data we need to test a particular code segment.
Check for the following code segment.
Resource resource = new Resource(); resource.setId("id"); resource.setName("Resource Name"); resource.setVersion("Version 1");
The values defined here will make the test pass and even for the coverages. But what if the actual usage of these values is not like “id”, “Resource Name”, “Version 1”. That unit test is failed in the functional aspect. This doesn’t explain how the Resource is going to be in the real world.
Let’s check below example.
Resource resource = new Resource(); resource.setId("AEWS-5444"); resource.setName("French:Status"); resource.setVersion("AEWS-5444-2");
Here we can get an idea about the Resource on a more detailed level. This explains the way of actual values since those values most probably come with a business meaning.
These are the stuff I thought of sharing on this article. Let me know if this was useful to you 😄