Sitemap

Writing Good Tests

5 min readJun 10, 2018

--

Why, How, and Evaluation

Why?

To summarize, the benefit of writing test is to make your program more scalable later time. Scalability of your program comes with its flexibility in design and implementation of every level of design and implementation, from statements to architecture. And when the program is not flexible, the road block is the amount of potential bugs that you may produce through a change made in your codebase. The opportunity cost you need to pay for fixing bugs introduced by a new feature accumulate if you do not think about this seriously as you add mode lines in your code.

The tests attest every units of your code to be adoptable with any potential behavioral expectations. Input data availability, data types, value range, configurations, constants, change all the time. If a function does not consider these factors, sooner or later, it bubble up as a bug to prevent your program to behave correctly. A test is a encapsulation of the expected state change of your program interacting with the part of your program you are testing against. It makes the unit of your program to be predictable for its behavior given variety of state change you may expect in the future.

Reducing the cost of sneaking bugs preventably is obvious benefit of writing tests but counter-intuitive one is that the test allows programmer to write better code in design and implementation faster. This is because tests encourage programmers to think of the usage of the method and its interaction with other components of the program before designing and implementing it. Clear understanding about the goal of the method and the anticipated state the method is utilized is the key to plan how it should behave in its context.

Test allows a programmer to assess the usability of the method objectively. Code of quality can be measured mainly in its correctness, performance, and readability but if there’s anything can be added in this list here, it would be usability. Test regards a portion of your codebase as if it is an small independent software that serves a goal of its user who consumes it. The user is the programmer, yourself or other programmer, who write a code along with the function. Tests gives better understanding the requirements and usage of the method and tells what quality should be satisfied, not too much or too less.

How?

When we should write a test?

Fowler said:

Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.

Whenever you’re implementing a class or method you realize the necessity to figure out what’s going internally. This indicate you have a blind spot about the internal implementation which introduced by its ambiguity of inputs, outputs, and behavior of public methods used in the method. Writing test for the method clarify the inputs/outputs requirements. If no test exists for the public method used then you now the time to write the test for it to disambiguate its inputs/outputs. Most of the case, the class or method you are going to write has a certain level of ambiguity before it is actually implemented, therefore, it’s much more likely you write a test before write a code.

What for should I test?

It’s important to know the scope of tests.

  • Unit test tests a class or method, independent unit of the functionality for a module.
  • Integration test tests a interaction of a set of classes and methods, independent unit of the functionality of a feature.
  • Regression test tests a new state brought by a change in its codebase, independent unit of the functionality of a entire codebase.
  • Acceptance test tests a user experience, independent unit of the functionality of a product or service.

As you write any code, if you expect a test to be unit test but write in the way it fallen in the category of integration tests (testing more than one class or method), it should raise a flag about how you test things.

Get Taka Ishikawa’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, for unit test, it is written in the way that:

  1. test one method
  2. provide some specific arguments to the method
  3. confirm the expected result returned

It should be designed and implemented independently satisfy the methods’ usage requirement. To achieve this:

  • no use of external method to produce the expected result. hardcode it instead.
  • simplify a test. write as many test as you need to cover the interesting code path to see its coverage.

You basically can apply the same principle of the independence to other scope of tests too. Never scope out too much (write unit test as if it’s integration test). Scope in as much as you can as long as it does not break down the purpose of the tests too much (write integration test as if it’s a set of unit tests).

Evaluation?

To keep track of improving the tests quality of a project, there should be a set of measurements to evaluate. This sounds tough but when we get back to the original reasoning behind why we should write test, ideas comes down more easier than you think.

One of the scoring measure for it is the time spent for bug fixes per a new feature introduced. If the tests strategy is well designed and executed, we see better score for the feature scalability in your code. If the team spends too much time to extinguish fires per new feature implementation as your codebase grow, it signals the poor quality of the code in design and implementation. If tests coverage is below a certain level, increasing it may improve this feature scalability score. Not only the coverage, quality of the tests should also be assessed using the measure of independence's for each scope of testing (unit, integration, regression, and acceptance).

The other is, thought it’s more qualitative measure, how the engineers in the team feel each component of code is usable and easy to develop upon. Majority of the code, if it severs as a production or working in staging, satisfy its correctness and mostly performance. Readability is a clearly measurable regardless of its existence of tests. But usability of the components of the codebase can vary with or without the tests and its quality. If you ask around the engineers, especially new engineers working on the codebase, then they raise complains about its codebase quality, it might be good time to consider utilizing tests as a strategy to improve it.

Conclusion:

Any software product is decomposed into a set of independent classes and methods. Regardless it’s complexity, without clearly define its usage and requirements beforehand, it’s quite easy to produce unexpected behaviors of your software in its interactions between those components. Tests are here for giving clearer understanding about the expected and actuals of those independent components and its interactions. It makes the software more scalable by reducing the cost of bugs and boost the velocity of development in long-term.

References:

--

--