Part1: Testing in Android

Ahmad Kazimi
4 min readJun 15, 2022

--

We’ll talk about testing in Android (Unit test, Integration test, and UI test)
and how to write a good test

What is testing and why do we write them ?!

Test cases are checking if our code really works as it should.

I believe you are already doing testing, but in a different way,
when running your app and clicking around to check if everything is working fine.

but what is the problem with that approach? the big problem with this approach is detecting problems one time. and whenever you want to check again you have to open your app and repeat the same steps again and again. I know we don’t have time⏰ for this.

Junit

Now here is where Junit comes to play

JUnit: is a very popular testing library used to save that time for us
and test your code with one click and automate those test scenarios.

this happens by Writing test cases telling our program to do this or do that and afterward, this condition must be true or false it depends on your business logic.

sound good so far?

Now let's talk about the types of testing we can categorize testing into 3 main categories.

1. Unit Test
2. Integration Test
3. UI Test

Testing pyramid

1. Unit Test

It’s made up the base of the testing pyramid and basically, it tests a single unit or single component of our app e.g. function of a class, and its the fastest test in our app.

unit test should cover 70% of our app test cases

2. Integration Test

It’s testing how different components of our app work with each other
for example how the Fragment interacts with the ViewModel

🧨 very important note don’t mix between an integration test and an instrumental test.

Integration test should cover 20% of our app test cases

2. UI Test

also called end-to-end testing.

UI test is the term used for any test that verifies the correct behavior of a UI. They are usually instrumented tests that run on a device or emulator and check through instrumentation that the app behaves correctly.
for example, check if the checkbox is checked or if recycler view has a specific item, and so on.

🧨 very important note don’t mix between an integration test and an instrumental test.

Integration test should cover 10% of our app test cases

How to write a good test?

TDD

You have heard of TDD? TDD means Test Driven Development

Main principle: Write the test case before implementing the actual function. (Unit test only)

  1. define the function signature without implementing the function content.
  2. Write a test case for that function we just declared.
  3. Finally, we need to implement the function content to pass that test case.
TDD cycle

Single Assertion per test case

to have a good test case also you should have only one assertion per test.

Assertion: is a line of code that guarantees that the condition we need is verified.

This helps us to spot the problem immediately we don’t want to analyze the test case itself

Some time there’s no way around the multiple assertion but please keep them as low as you can

There’s another important topic when we come to the good test and its the
Characteristics of test

Characteristics

  1. scope: Determine how much of the actual code in our function we want to test is covered by our single test case.
  2. Speed: Self-explanatory How fast our test case run.
  3. Fidelity: It means how much our test case is close to the real scenario.

Not a Flaky tests

Flaky tests: Those tests are sometimes successed and sometimes fail.

How could this happen ! For example, if we generate a random number and we have a test case checking if the number is 2. if the generated number was 2 we have a successful test case otherwise it will fail.

we need to prevent Flaky tests.

NOTE 🧨 : Never make the outcome of a test case depending on the outcome of another test case.

That is, for now, see you in Part2: Testing in Android

Happy Coding 😊
Kazimi

--

--

Ahmad Kazimi

Turning pizza and coffee into magic, also called Android apps.