Do you really test what you want to test?

Yahya BAYRAMOĞLU
Aug 23, 2017 · 3 min read

Testing is already a huge topic, and it is really important for everyone. So, that is not even open to discussion anymore. If you disagree, please start searching on Google why you should test.

Since we agree... Do you really test what you intended? How do you make sure you test the right thing?

If you really follow TDD then it is even better, but if not; writing tests for the code you wrote by yourself is crucial. Because you believe yourself, you wrote that code, it is simple, what are you going to test? You just need to write tests so that people don’t complain that you don’t. Right?

Please tell me you said: “No!

The important point is here to know what do you really want / need to test. And that is where most people fail.

Let’s go step by step on how to find out what you need to test.

  • Know your business logic.
  • Know your expected behaviour.
  • Know your scope.

Business logic and behaviour are already well explained in a lot of articles. So, what I really want to focus on is the scope!

Keep it as simple as possible

Don’t get amused by all the possibilities you can test. Don’t try to write them all in once and try to split / break-down them as much as possible.

What does that mean?

Let’s assume you want to test this kind of business logic;

fun veryImportantLogic() {
if(decisionA()) {
behaviour1()
} else {
if(decisionB()) {
behaviour2()
} else {
behaviour3()
}
}
}

And then you write one single test to cover every possible decision and behaviour.

— Nope, don’t do that!

Create separated tests for each decision and expected behaviour. Because all of them are actually different user stories and they deserve their own test cases.

Since this is a very simple code snippet -but very important logic, as its name states 🙄- it is easy to see that expected behaviours should be covered with tests. So a good approach to tackle this would be creating 3 different test cases and write tests for each. Such as;

  • decisionA is true.
  • decisionA is false, but decisionB is true.
  • both decisionA and decisionB are false.

Find the focus of the test

Think about a very unique subject that needs to be tested. Do not think in bigger dimensions. The smallest, but the most important part you need to test. What is it?

Maybe you have some tracking specifications and you want to test them. You want to know if right tracking parameters are sent.

  • paramA, paramB and paramC are general information and they need to be tracked with every tracking call.
  • paramD and paramE are unique tracking parameters which need to be tracked only when the user does some click action.

First of all, you need to find a way to test that paramA , paramB and paramC are appended as required for every tracking. This would mean, you should be able to write a single test to verify that this global appending is happening.

But that doesn’t mean, you have to check those parameters for every single tracking that you send.

In this sample, when the user clicks on something that you need to track, you would focus on required parameters but not on the rest. So here, your focus should be on action specific parameters, but not the whole parameters that you will have at the end. (In an ideal world, you shouldn’t even know all the parameters that will be at the end anyway.)

With this approach, you also narrow down your test case, so even if some other requirement changes it will break a test, yes. The general one, the one you covered as appending part. But not every other tracking tests.


Keep your tests simple and focused.

And don’t forget if you need to update your tests all the time when something is changed in one place, that would mean your test’s scope is too wider than it should be. Prune that!

)

Thanks to Anablava Ligataj

Yahya BAYRAMOĞLU

Written by

Android Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade