Unit Test Android apps using the JUnit framework 20150412

Source: Internet
Author: User

My last blog http://www.cnblogs.com/wushaobo/p/4396308.html, developed a simple Android app leap year test. Here, you use the JUnit framework to perform simple unit testing of the app.

In order to facilitate the test, I made a simple modification of the program, the toast prompt to the page text, add the return event response leap year judgment. The effect is as follows:

1. Test Preparation

In real-world development, the process of developing Android software needs to be constantly tested. Using the JUnit test framework is a must-have technology for regular Android development.

Testing using the Eclipse + ADT +android SDK as a development environment

1). First create a new Android test project, I'll name it Test

Specify the items you want to test

2). Configure the framework

Open the Androidmanifest.xml manifest file under the test project, copy and paste the code from the following red boxes into the androidmanifest.xml manifest file for the Leap year test project, and the copied nodes must be accurate.

3). Create Test Cases

Create a new package under the test project

Create a new JUnit test case in this package

Make this class inherit the Android.test.ActivityInstrumentationTestCase2 class

4). Create a constructor function

1  Public class extends Activityinstrumentationtestcase2<mainactivity> {23public       Testleap (String name) {4         Super(mainactivity).  Class); 5     }6 }

Right-click the Overwrite implementation method, select the Setup () and the Teardown () method from the source code. Do some initialization of the global variable when the class is created.

5). Writing test functions

The final code is as follows:

1  Packagecom.leap.leapyear.test;2 3 Importcom.leap.leapyear.MainActivity;4 ImportCOM.LEAP.LEAPYEAR.R;5 6 ImportAndroid.test.ActivityInstrumentationTestCase2;7 Importandroid.view.KeyEvent;8 ImportAndroid.widget.TextView;9 Ten  Public classTestleapextendsActivityinstrumentationtestcase2<mainactivity> { One  A     PrivateTextView result; -  -      Publictestleap (String name) { the         Super(Mainactivity.class); -     } -      - @Override +     protected voidSetUp ()throwsException { -         Super. SetUp (); +Mainactivity mainactivity =getactivity (); Aresult =(TextView) Mainactivity.findviewbyid (r.id.result); at     } -  - @Override -     protected voidTearDown ()throwsException { -         Super. TearDown (); -     } in      -      Public voidTestleapyear ()throwsexception{ to SendKeys (keyevent.keycode_2); + SendKeys (keyevent.keycode_0); - SendKeys (keyevent.keycode_1); the SendKeys (keyevent.keycode_2); * SendKeys (keyevent.keycode_enter); $String Leapresult =Result.gettext (). toString ();Panax NotoginsengAssertequals ("is a leap year", Leapresult); -     } the  +}

6). Run Unit Tests

Right-click the Debug function to run

7). Test results

Showing the green bar in the JUnit console proves the test passed.

Modify test case to illegal input then test does not pass

Unit Test Android apps using the JUnit framework 20150412

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.