Wednesday , June 22 2022

Android Unit Test Tutorial – Writing Your First Unit Test – Android Coding by DMTechnolab

Hey guys, so you can write or code Android apps. But do you test them? Yes, I know that everyone compiles and watches the app, whether it is working or not. But do you write code to test? Do you test every function and every module? If you know your answer, this Android unit test tutorial is for you.

I think you want to write tests for the app now, and don’t worry if you’re not sure where to start; I will cover everything related to testing in this series post.

Now before proceeding further, you should go through this video, to understand about testing in Android development.

In this post you will learn to write your first unit test. So let’s start without wasting time.

What is Unit Testing?

Unit testing is a testing method where we test individual codes (or the smallest unit) for a code; To ensure that they are working properly.

Unit testing helps confirm that every single unit of the application’s code is working as expected.

Unit tests are often written even before code is written. In other words, for each function that has to be created, a unit test is written before writing the function. By following this approach, we can fix potential insects at an early stage of development.

Benefits of unit testing

unit test,

  • Helps find bugs early.
  • Since it helps in finding insects in the early stage of development, it also reduces the development cost and time.
  • Simplifies refactoring and provides documentation.

Unit testing is not specific to any platform such as Android or Web. It is basically a concept that is used in software development. But here our focus is unit testing in our Android projects.

Android unit test tutorial

Now, let’s discuss our main topic Android unit test.

Whenever you create an Android project, you see these three packages.

Android unit test tutorialAndroid unit test tutorial
Android unit test tutorial

Here you can see that you have the main package (the first one), and inside this package we put all our application code. But with this package we have two more packages. You can differentiate these packages with the hint shown AndroidTest and testing.

Now we will not discuss about AndroidTest. Our objective today is only the test package. Inside this package we write all the code for unit test.

Go to app level now Build.shield See file and dependency blocks.

You can see our here testing And AndroidTestImplementation . testing There are libraries available inside the test package. And in the same way if you want the library to be available inside the AndroidTest package you need to use AndroidTestImplementation.

Now Java Unit Testing Framework JUnit It is added here by default. Whenever you create a project it is added by default. We will use the unit to test our code.

But with JUnit we will use Google Truth to simplify the statements. Since it is not available by default; We need to add it inside the app level dependency block Build.shield File.

Code for unit testing

For this example, I have a very simple task that we will test.

This is a very simple code. And to write tests for this, we need to create a file inside the test package. You can create the file manually or you can follow these steps.

  • Right click on the class that needs to be checked.
  • Go to Generate and then select Test.
  • For now select JUnit and hit OK.

Writing unit test

Now finally we will do our test To verify Function.

You can use the Play button in Android Studio to run the test. You will see the play button before every function and class.

Android unit test tutorial 1Android unit test tutorial 1
Android unit test tutorial

As you can see in this image. Just run the test and you will see it pass the test. If the test fails, then you need to make changes on your function.

So whenever you change anything in the function, you need to test it again. And thus your code will not break with future changes. Because it should always pass the exam.

Android unit test tutorial source code

I am building a complete test series, and in this series we are building an application using the test driveen development approach. And to prepare for the next lesson, you should get the source code of the project from here.

So that’s all for this post, stay tuned for the upcoming tutorial as we are going to cover a lot of related things Test in android development. Thank you 🙂

Belal Khan ProfileBelal Khan Profile

Hi, my name is Belal Khan and I am Google Developers Specialist (GDE) for Android. A passion for teaching made me this blog. If you are an Android developer, or you are learning about Android development, I can help you a lot with simplified coding.

Source link

About dmtechnolab

Check Also

Simplified Coding

Android Hilt Tutorial – Injecting Dependencies with Hilt – Android Coding by DMTechnolab

You are welcome Android Hilt tutorial. I hope you all are aware of it Dependency …

Leave a Reply

Your email address will not be published. Required fields are marked *