Simple unit test for Android

Source: Internet
Author: User

I have accumulated many articles recentlyArticleIt hasn't been released yet. It's very late today. Let's write it here. Do not forget the notes of the articles to be sent!

As follows:
Google location, add tags!
SQLite database operations and Summary of data storage (SQLite, Bundel, sharedpreferences)

QR code encoding and decoding, zxing uses full resolution

................

Now let's get down to the point. Here I will briefly talk about unit testing!

 

1. Create a test project:
Create an android test project
Select the project you want to test in test target.
2. Create a test class
Inherit activityinsrumentationtestcase2 <Helloworld> is the activity you want to test in the project.
Iii. Write TestCode
The method starts with test, such as testabc and testtank. Note the naming rules.

Code:

 

Code

Package Com. example. Android. test;

ImportAndroid. Test. activityinstrumentationtestcase2;
ImportAndroid. widget. textview;

ImportCom. example. Android. helloworld;

PublicClassHellotestExtendsActivityinstrumentationtestcase2<Helloworld>{

private helloworld mactivity; // the activity under test
private textview mview; /// the activity's textview (the only view)
private string resourcestring;

PublicHellotest (){
Super("Com. example. Android", Helloworld.Class);//Class

//Todo auto-generated constructor stub
}

@ Override
Protected Void Setup () Throws Exception {
Super . Setup ();
Mactivity = This . Getactivity (); // Obtains the activity instance of the class passed in by the constructor.
Mview = (Textview) mactivity
.Findviewbyid(com.example.android.r.id.txt ); // Get the view object in the helloworld project.
Resourcestring = Mactivity
. Getstring (COM. example. Android. R. String. Hello );
// Resourcestring = "tank ";
}
Public Void Testtext (){
Assertequals (resourcestring, (string) mview. gettext ()); // Whether the value of textview is the expected value (yes, actually)
}
Public Void Testpreconditions (){
Assertnotnull (mview ); // Judge whether it is null
}

}

Assert class encapsulates a lot of methods to provide our test use, you can view the source code, clear follow me a few articles have talked about in myeclipse to view the source code method http://www.cnblogs.com/tankaixiong/archive/2010/10/20/1856899.html
Method list: asserttrue (), assertfalse (), assertsame (), assertnull ()............

The test result is as follows:

 

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.