Eclipse IDE Android test project

Source: Internet
Author: User

Because I was a test engineer, I just switched to mobile phone Development and wrote about it for a few days.CodeSo these days I have always been eager to write things, so I decided to write an article on how to do android unit testing and start my first Android testing project:

  1. Some resources:
    1. Google development documentationYou can see it only when you go through the wall, but there are many places where you can download offline packages.
    2. The rest is the android development environment. See the development documentation for your own configuration.
  2. Next, we will first create an android project, which implements some functions according to the normal development process. I have some operations on Android connections implemented in the project, because I also need to verify whether my operations are actually valid, at first I added a test button on the interface and wrote some call methods in this button, every time a function is tested, the implementation in this button is deleted, and new code is written. This is very inconvenient, and the "function" of the original software is damaged. It should be tested anyway, why not write real test code? In addition, writing test code is also my old line. The real test code will be started below:
    1. Create a test project: click "file-> New-> Project" in the menu, and select Android test project.Enter the name of the test project and select the project to be tested. The test project is created.
    2. After the test project is created, you can see that the general development project of the test project structure is similar. Then, create a JUnit test case under the test project package and write the name of the test case.Here, we select new JUnit 3 test, setup () and teardown () to check. Of course, you do not check them either. You just need to write them in the test code.
    3. The newly created test class is based on the testcase class, but we need to use some "functions" of the software during the test, so we need to make some modifications as follows:
       
      1:

       
      2: Public ClassTestpersoncantactExtendsActivityinstrumentationtestcase2 <mainactivity> {

      3:

       
      4:PrivateContext CTX;

       
      5:

       
      6:PublicTestpersoncantact (string PKG, class <webshare> activityclass ){

      7:Super("Com. Android. contaxt", Webshare.Class);

       
      8:// Todo auto-generated constructor stub

       
      9:}

       
      10:

      11:PublicTestpersoncantact (){

       
      12:Super("Com. Android. Contact", Webshare.Class);

       
      13:}

       
      14:

      15:Protected VoidSetup ()ThrowsException {

       
      16:Super. Setup ();

       
      17:CTX = getactivity ();

       
      18:

       
      19:}

       
      20:

       
      21:Protected VoidTeardown ()ThrowsException {

       
      22:Super. Teardown ();

       
      23:}

    4. Now we have completed the preparation of the test case. Remember to write the two constructors correctly. Otherwise, there will be problems. Next we will start to write our test case, because it is the first time we use JUnit, we found that the test case is a bit like python, and the function name format is testxxxx, in gtest, a macro is used for representation. Well, we can write a method like:
 
1: Public VoidTestdeletepersoncontact ()ThrowsNoidexception {

 
2:Contactshelperhelper =NewContactshelper (CTX );

 
3:Personcontact one = personcontact

4:. Newpersoncontact (TestPersonContact@gmail.com");

 
5:Personname name = personname. newpersonname ("Firstname","Lastname",

 
6:Null, null, null );

 
7:Telephonenumber Tel = telephonenumber. newtelephonenumber ("123123123",

8:3, null );

 
9:Telephonenumber tel2 = telephonenumber. newtelephonenumber ("1231231230",

 
10:1, null );

 
11:

 
12:One. Name = Name;

13:One. addtelephonenumber (TEL );

 
14:One. addtelephonenumber (tel2 );

 
15:

 
16://... Email ,...

 
17:Try{

18:Helper. Add (testguy );

 
19:}Catch(RemoteException e ){

 
20:// Todo auto-generated Catch Block

 
21:E. printstacktrace ();

22:}Catch(Operationapplicationexception e ){

 
23:// Todo auto-generated Catch Block

 
24:E. printstacktrace ();

 
25:}

 
26:

27:Personcontact Pc = helper. getpersoncontactbyid (one. ID );

 
28:

 
29:Try{

 
30:Helper. Delete (PC );

 
31:}Catch(RemoteException e ){

32:// Todo auto-generated Catch Block

 
33:E. printstacktrace ();

 
34:}Catch(Operationapplicationexception e ){

 
35:// Todo auto-generated Catch Block

36:E. printstacktrace ();

 
37:}

 
38:

 
39:Personcontact pC1 = helper. getpersoncontactbyid (PC. ID );

40:This. Assertequals ("Telephonenumbers after Delete count is :"+ Pc1.telephonenumbers. Size (), pc1.telephonenumbers. Size (), 0 );

 
41://...

 
42:}

JUnit also provides many methods such as assertequals and asserttrue, so that we can quickly complete the implementation of the test case code. I will write a few in one breath to see the running effect as follows:

I am so happy to look at the beautiful green lines.

 

When writing code, you can also develop the habit of writing test code. You can organize and archive a lot of scattered test code you have written, after reading your test code, someone else will know how to call your interface. You don't have to tell others how to use this interface once? Isn't that good? ^_^

 

 

 

 

 

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.