Ads Top

Unit Testing Is Important But Hard

Quick Summary
    1: Unit testing is good and you should do it
    2: Its hard and forces a different way of thinking
    3: Don't over complicate things
What is unit testing?
Testing for web apps is typically broken down into a pyramid structure that looks like this. The bottom of the pyramid is the biggest block as it is typically the largest chunk of code.

While all are important, as a developer your first and largest focus should be on unit testing. It is called unit testing because we want to test individual units of source code. And the reason we want to do this is to make our code more manageable and maintainable as well as more predictable when making a change either large or small.

For example, have you ever been tasked with making what you and other believe to be a "small" change? Then you open the file, see hundreds of lines, find the area you need to make the change, and you end up breaking 3 other things just from this one small change? If you are lucky, you catch that error before releasing, but most of the time, you may not be so lucky.

Adding a testing layer would allow for most of these issues to be caught beforehand.

Do you write unit tests?
When presented with this question, say in an interview setting, most developers will say YES. However, if you pay attention to most codebases at any company you have been to, the answer should almost always be rephrased to .... Sometimes. Or, we try to.

Why not always unit test?
If everyone starts with good intentions, and most people know the benefits of unit testing, then why does it seem that so many codebases have no tests, or minimal amounts of coverage?

1: Not having a full grasp of the problem you are trying to solve
In order to write good tests, you need to have a firm understanding of what you are building. Often times, this can be harder than you think especially when it comes to projects where you have outside interests dictating the business rules and functionality. Not having a firm understanding of what it means to be "DONE" can lead to a lot of moving targets and quick changes.

To start unit testing successfully, you really need a clear understanding of what you are building. This will allow you to break problems down into small testable units.

2: Unit testing is hard
Take point number one. Even if you have a clear vision of what you are building, testing itself can be hard. Of course, just about every example online has your typical easy examples of testing a function that adds two numbers, but in reality, professional projects are much more complex and detailed. We have many functions, API calls, conditionals based on rules, etc. You often spend a lot of time trying to figure out how to code the app itself that the tests become secondary.

3: Business and end users do not see tests
The business and the end user doesn't care about tests for the most part. They just see an application, and from that, they expect it to work. Testing really is, in most part, for developers by developers. It helps you think about the problem you are solving and the code you are writing to solve it in smaller pieces. When it comes down to it, its more important to push your code that the user sees than worry about if the test is up to date.

Of course this can backfire if you make a hasty change without having a test (or remove failing tests) just to get something out fast and it then fails due to some unforeseen error.

4: Psychological Aspect
Your mind may make you and others believe that writing tests will take more time.
Initially you may look at writing tests as writing more code. More code would equal more time. So it may seem counterintuitive to say that a test first approach can decrease overall development time.

Keep in mind, because you are breaking down what you are building into small units, it makes it easier to absorb. That function that was 50 lines when you were just writing code, might be 20 lines if properly thought out with testable units.

When unit testing is important
1: multiple people working on the same code base
2: code you are going to have to revisit often
3: code that had high amounts of business logic
When testing is not as important
1: "throwaway" scripts or projects that are one offs

With this knowledge, what is a good resource to start your unit testing journey? Here is a good video series to get you started. From there, I'm sure youtube suggestions will take over.

No comments:

Powered by Blogger.