Unit Test for Android development (1)

Source: Internet
Author: User

Unit Test for Android development (1)

Unit Test for Android development (1)

Please respect others' labor achievements. Reprinted with the source: unit test for Android development (1)

Http://blog.csdn.net/fengyuzhengfan/article/details/40209995

In actual development, the process of developing android software must be continuously tested. Android unit testing is a required step for formal Android development. Unit tests can be embedded into a project. You can also test a specific project as a separate project.


1. hierarchical structure of the Android unit test framework

We can see that:

1. Test Methods in android include AndroidTestCase and InstrumentationTextCase. So what is Instrumentation?

The Instrumentation and Activity are a bit similar, but the Activity requires an interface, and the Instrumentation is not like this. We can understand it as a kind of graphic interface without startup capability, tool class used to monitor other classes (declared using Target Package.

2. android testing can be used to test important android components (such as Activity, Service, ContentProvider, and Application ).


2. Steps for Android Unit Testing


Here I write a simple tested class MathUtil. There is only one tested method in this test class. sum is used to calculate the sum of the two integers.

Package com. jph. simpleapp;/*** tested class * @ author jph * Date: 2014.10.18 */public class MathUtil {public MathUtil () {// TODO Auto-generated constructor stub} public int sum (int a, int B) {return a + B ;}}

Tip: The method to be tested must be declared as public; otherwise, the method cannot be accessed.

The following describes how to create a test project:

Step 1: Create a test project


Create an Android Test Project. In the pop-up box, select Android Test Project,

Select the project to be tested and click finish.

Now, the Android test project has been created. Let's take a look at its project list file,

It can be seen that there are more Android test projects than ordinary Android projects. Labels and "Android. test. runner"/>.

TargetPackage indicates the package name of the Android project to be tested.

Step 2: Create a test class


Here we choose to inherit from AndroidTestCase.


Test code:

Package com. jph. simpleapp. test; import com. jph. simpleapp. mathUtil; import android. test. androidTestCase;/*** test class ** @ author jph * Date: 2014.10.18 */public class MathUtilTest extends AndroidTestCase {MathUtil mUtil;/*** the first method to be executed, you can perform some initialization operations in this method * @ see android. test. androidTestCase # setUp () */@ Overrideprotected void setUp () throws Exception {// TODO Auto-generated method stubmUtil = new MathUtil (); super. setUp ();} public void testSum () {int result = mUtil. sum (2, 1); assertEquals (3, result); System. out. println ("result:" + result);}/*** the last method to be executed. Here we will do some final work. * @ see android. test. androidTestCase # tearDown () */@ Overrideprotected void tearDown () throws Exception {// TODO Auto-generated method stubsuper. tearDown ();}}

Code Analysis:

SetUp (): the first method to be executed. You can perform some initialization operations in this method.

TearDown (): The last method to be executed. Here we will do some final work.

TestSum (): The method to test sum.

Now, the test class has been compiled. Next we will start the Android unit test.


Step 3: Perform unit test


You can choose whether to run a test method or all the methods as needed.

Running result:


Result Analysis:

We can see that the running result is failed because 3 is returned by sum, but we expect it to return 4.


Below we will change the expected value to 3:



At this time, it is expected to be the same as the result, and the operation passes. We add the previous values 1 and 2 and return 3. Therefore, the sum method is correct.

To be continued .............


If you think this blog post is helpful to you, please give it a compliment! You can also follow fengyuzhengfan's blog to watch more exciting http://blog.csdn.net/fengyuzhengfan/


Related Article

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.