Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Testing and securing android studio applications

You're reading from  Testing and securing android studio applications

Product type Book
Published in Aug 2014
Publisher Packt
ISBN-13 9781783988808
Pages 162 pages
Edition 1st Edition
Languages

Chapter 9. Unit and Functional Tests

You already learned about Android testing in the previous chapter. You know how to develop a black-box test of the UI of your application. Now you want to learn how to implement the white-box testing for your application. Are there different types of activity testing? Does Android Studio support activity testing? How can you get the results of your test cases? We will be covering these points in this chapter.

In this chapter, you will learn how to use unit tests that allow developers to quickly verify the state and behavior of an activity on its own. The chapter will also cover functional tests; their main purpose is to check the interaction between components.

The topics that will be covered in this chapter are as follows:

  • Differences between unit and functional tests

  • Android testing API

  • Creating a simple unit test case

  • Creating a simple functional test

  • Getting the test results

Testing activities


There are two possible modes of testing activities:

  • Functional testing: In functional testing, the activity being tested is created using the system infrastructure. The test code can communicate with the Android system, send events to the UI, or launch another activity.

  • Unit testing: In unit testing, the activity being tested is created with minimal connection to the system infrastructure. The activity is tested in isolation.

In this chapter, we will explore the Android testing API to learn about the classes and methods that will help you test the activities of your application.

The test case classes

The Android testing API is based on JUnit. Android JUnit extensions are included in the android.test package. The following figure presents the main classes that are involved when testing activities:

Let's learn more about these classes:

  • TestCase: This JUnit class belongs to the junit.framework. The TestCase package represents a general test case. This class is extended by the...

Creating an activity test


In this section, we will create an example application so that we can learn how to implement the test cases to evaluate it. Some of the methods presented in the previous section will be put into practice. You can download the example code files from your account at http://www.packtpub.com.

Our example is a simple alarm application that consists of two activities: MainActivity and SecondActivity. The MainActivity implements a self-built digital clock using text views and buttons. The purpose of creating a self-built digital clock is to have more code and elements to use in our tests. The layout of MainActivity is a relative one that includes two text views: one for the hour (the tvHour ID) and one for the minutes (the tvMinute ID). There are two buttons below the clock: one to subtract 10 minutes from the clock (the bMinus ID) and one to add 10 minutes to the clock (the bPlus ID). There is also an edit text field to specify the alarm name. Finally, there is a button...

Summary


In this chapter, you learned more about Android testing. You now understand the structure of the Android testing API and we know its main classes and methods. You also learned about the importance of instrumentation to test activities of the Android applications. We set up the testing environment using Android Studio and followed the complete process of testing.

In the next chapter, you will learn about some external tools different from Android Studio. These tools will help us secure and test our Android applications.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Testing and securing android studio applications
Published in: Aug 2014 Publisher: Packt ISBN-13: 9781783988808
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}