Continuous Unit Testing in Android Development Using Bitrise

Nwokocha Wisdom Maduabuchi
The Startup
Published in
5 min readNov 19, 2020

Unit tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the “unit”) meets its design and behaves as intended.

In this tutorial, you will learn how about unit testing, its benefits, its types, unit testing tools, how to write a simple unit test in Android studio, and automating the testing processing using Bitrise.

What is Unit Testing?

Unit Testing is a type of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software code performs as expected. Unit Testing is done during the development (coding phase) of an application by the developers. Unit Tests isolate a section of code and verify its correctness. A unit may be an individual function, method, procedure, module, or object.

Types of Unit Testing

In software engineering we have two types of unit testing which are:

  • Manual
  • Automated

Benefits of Unit Testing?

  1. Unit testing will help the developer to fix/catch bugs early in the software development.
  2. It helps the developers to understand the codebase.
  3. unit-testing your project will help the developer structure the codebase and use an architecture
  4. you can test individual part of your code
  5. it saves time and cost in testing because you are using the JVM to run your test

Unit Testing Tools

There are lots of free tools that will assist a developer to test his code, below are a few of them:

Junit: Unit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development and is one of a family of unit testing frameworks which is collectively known as xUnit that originated with SUnit. learn more here

Robolectric: Robolectric lets you run your tests on your workstation, or on your continuous integration environment in a regular JVM, without an emulator. learn more here

Mockito: Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with a clean & simple API. learn more here

Truth: Truth is a fluent and flexible open-source testing framework designed to make test assertions and failure messages more readable. learn more here

Hamcrest: Hamcrest is a framework for writing matcher objects allowing ‘match’ rules to be defined declaratively. learn more here

How to do Unit Testing

Let's follow the step by step guide below in creating your first Unit test:

Step 1: Add the necessary dependency for Truth/Hamcrest
but in this tutorial, we will make use of Truth

repositories {
mavenCentral()
}
dependencies {
testImplementation "com.google.truth:truth:1.1"
}

Step 2: Create a Unit test class

Step 3: Write your Unit test
This entirely depends on what you want to test, if you check the image below you will see what I am trying to test for.

What I did was to create a function that does a specific task and returns a value, so I am checking for an “@” in the return value.

you will see I am getting a failed test because it didn't meet the requirement now lemme fix the bug by adding an “@” sign

Step 4: Add the Firebase Android Unit Test Step

To add it, go to the dashboard -> Click on the App then select the project you want to automate the testing process.

After selecting the app It will open the image below, then you Click on “Workflow”

The image below will show

Now to add a new Step, you will click on the plus sign in the Workflow

When you click the plus sign you will see many Steps to choose from, but in this tutorial, you need to select a Firebase Android Unit Test Step.

When you select the Firebase Android Unit Test Step the Image below will show.

Make sure you have the Deploy to Bitrise.io Step in your Workflow.
Once the build is finished, you will be able to view your test reports.

Then click save(X+S) and push your new changes to your preferred source code management(Github, Gitlab, Bitbucket, etc)
After a successful Build goto your dashboard and click on the build.

After clicking on the Build the below Image will open

To see your Android Unit Test report click on the “Test reports” button to get the below image.

Click on the “Test Case” button and the image below will show the test class names and duration

That’s how easy it is to automate unit testing in Android development using Bitrise.

Thanks for reading my article 😍😍😍😍👍

Let’s connect on
Twitter: https://twitter.com/Joklinztech
LinkedIn: https://www.linkedin.com/in/wisdom-nwokocha-76212a77/

--

--

Nwokocha Wisdom Maduabuchi
The Startup

A software engineer with considerable experience in mobile development, native Android, and IOS development(Xcode), flutter dev, technical writing and community