Getting started with writing functional test cases in Android

Bhavin Vala
Mindful Engineering
5 min readNov 11, 2021

Software testing is the process of evaluating and verifying that a software product or application does what it is supposed to do. The benefits of testing include preventing bugs, reducing development costs and improving performance.

Types of Software Testing

  • Functional Testing
  • Non-Functional Testing

What is a Functional Test?

Functional testing is a type of software testing that validates the software system against the functional requirements/specifications. The purpose of functional tests is to test each function of the software application.

By providing appropriate input, verifying the output against the functional requirements. It is a type of black-box testing as the application's source code is not considered during the testing process.

Why Functional Testing is Required?

The need for functional testing is vital in validating the quality and functionality of the software.

Through this software testing technique, quality assurance teams verify the software functionality based on the Software Requirements Specification (SRS) and ensure the system functions as per the user specifications.

Functional Testing VS Non-Functional Testing

  • Functional testing verifies each function/feature of the software whereas Non Functional testing verifies non-functional aspects like performance, usability, reliability, etc.
  • Functional testing helps to enhance the behavior of the application.
  • Non-Functional testing helps to improve the performance of the application.
  • Functional testing is easy to execute manually.
  • Non-Functional testing is hard to execute non-functional testing manually.

Advantages of Functional Testing

  • It ensures that the customer or end-user is satisfied.
  • It produces a defect-free product/software.
  • It ensures the all the requirements should be met.
  • It ensures the proper working of all the functionalities of an application/software/product.
  • It ensures that the software/product works as expected.
  • It ensures security and safety.
  • It improves the quality of the product.
  • Reduce risks and losses associated with the product/software.

Steps to Involved in Functional Testing

Step1 — The first step is to determine the functionality of a product that needs to be tested.

Step2 — Next step is to create input data for the functionality to be tested as per the requirement specifications.

Step3 — Output is determined for the functionality.

Step4 — Prepared test cases are executed in the next step.

Step5 — Compare the actual and expected output.

Types of Functional Testing

Unit testing:- It is a type of software testing where individual units or components of the software are tested. The purpose is to validate that each unit of the software code performs as expected.

Smoke Testing:- It is a software testing process that determines whether the deployed software build is stable or not. Smoke testing is also known as “Build Verification Testing” or “Confidence Testing.”

System Testing:- It is a level of testing that validates the complete and fully integrated software product. The purpose of a system test is to evaluate the end-to-end system specifications.

User Acceptance Testing(UAT):- It is a type of testing performed by the end-user or the client to verify/accept the software system before moving the software application to the production environment. The main purpose of UAT is to validate end-to-end business flow. It does not focus on cosmetic errors, spelling mistakes or system testing.

Regression Testing: It is defined as a type of software testing to confirm that a recent program or code change has not adversely affected existing features. Regression testing is needed, when a new feature is added to the software application and for defect fixing as well as performance issue fixing.

Integration Testing: It is defined as a type of testing where software modules are integrated logically and tested as a group. A typical software project consists of multiple software modules, coded by different programmers. This level of testing aims to expose defects in the interaction between these software modules when they are integrated.

Functional Testing Techniques

  • Positive Testing is a type of testing which is performed on a software application by providing valid data sets as input. It checks whether the software application behaves as expected with positive inputs or not. Positive testing is performed in order to check whether the software application does exactly what it is expected to do.
  • Negative Testing is a testing method performed on the software application by providing invalid or improper data sets as input. It checks whether the software application behaves as expected with the negative or unwanted user inputs. The purpose of negative testing is to ensure that the software application does not crash and remains stable with invalid data inputs.

Tools for functional Testing

  • JUnit
  • Mockito

JUnit is the most popular and widely used testing framework for java.

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
testImplementation 'com.google.truth:truth:1.1.3'
  • The integration of this dependency will allow us to write test cases cleaner and flexibly.

Truth is owned and maintained by the Guava team. It is used in the majority of the tests in Google’s own codebase.

val result = ValidatorUtils.validateRegistrationFields("", "123456", "123456")
assertThat(result).isFalse()
  • faster to type, especially with autocompletion.
  • easier to read.
  • less boilerplate code.

Let’s start with a simple example of a unit test to test email and password fields validations.

Create a ValidatorUtilsTest.kt file app/src/test/java inside your android studio project.

Never use '' single inverted commas for function name. It’s meant to be used for Unit tests only.
  • To test any function you can annotate that function with @Test.
  • Create input data for the functionality that need to be tested as per the requirements.
  • Output is determined for the functionality under test from the requirements specification.
  • assertThat is used for assertion, it is used to make sure that your result is true or false
  • Test cases are passed then you can write an actual function for your functionality.

Mockito is a popular open-source framework for mocking objects in software tests. Using Mockito greatly simplifies the development of tests for classes with external dependencies.

A mock object is a dummy implementation for an interface or a class.

Creating mock objects with the Mockito API

  • Using the static mock() method.
  • Using the @Mock annotation.
  • Create a mock object with the use of @Mock or mock()

The when(…​.).thenReturn(…​.) method chain is used to specify a return value for a method call with pre-defined parameters.

Let’s start with how to write test cases for firebase functions.

  • Implement Mockito dependency in build.gradle .
implementation "org.mockito:mockito-core:3.5.13"
implementation "org.mockito:mockito-inline:3.5.13"
  • Here is the repository implementation.
  • To handle the callback of the firebase function we need to create an interface.

Thank you for reading. Hope you enjoyed it!

--

--