Wise Engineering
Published in

Wise Engineering

5 Tips for Getting More Out of Your Tests

Still from Ridley Scott’s Blade Runner

Rule 1: when spec is more than 120 lines then split it

When the spec is too long we will not be able to grasp it quickly anymore. Specific number does not matter but I have found around 120 lines to be a good threshold for myself. With very large test file it gets hard to detect duplication/overlap when adding new test methods. Also it becomes harder to understand the behaviour being tested.

Rule 2: when test names have duplication it is often a sign that you should split the spec

Typically unit tests are 1:1 mapped to each production class. So tests often need to specify what exact part of the target class is being tested. This is especially common for the above mentioned Services which are often just collections of different kinds of procedures.

Rule 3: instead of splitting too long spec think if something should change in production code instead

If there are many tests for something then it means that the tested behaviour is complex. If something is complex then it should be split apart. Often lines of code are not good indicator for complexity as we can easily hide multiple branches/conditions into a single line.

Rule 4: when test contains a lot of interactions then introduce some new concept in the production code

When looking at the tests for such Transaction Script Services then often they contain a lot of interactions. This makes writing tests very hard as it should be because there is clearly too much going on at once and we are better off splitting it.

Rule 5: extract new class when you find yourself wanting to stub out a method in tested class

When we think that we need to mock/stub some class partially then this is generally a bad idea. What the test is telling us is that we have too much behaviour cramped together.

  • don’t mock it and use the production implementation
  • if our test becomes too complex or we need too many similar tests then extract that logic out into a separate class and test that part of behaviour separately

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ürgo Ringo

Have been creating software for 20 years. Last 7 years worked as IC and team lead at (Transfer)Wise. Currently working as VP of engineering at Yummy.