Android Test Tutorial (9): Applicationtestcase sample

Source: Internet
Author: User
Tags final

Some of the fundamentals of Android testing are described earlier, and several examples of Android testing will be presented in conjunction with the apidemotest example. Before this you can refer to the Android Test tutorial (3): Test project to create Apidemos->tests test project, this project test is used to test Apidemos, the main purpose is to introduce the use of the Android test framework.

Of course to test Apidemos, in advance to create a good project Apidemos. The following illustration shows the Java classes contained in the apidemos->tests after the creation of the apidemos->tests:

The Android test project is also an Android application, and its basic usage is very similar to the development of a generic Android application, and the Android test program uses the Android test framework to write test cases to test all aspects of the corresponding Android application.

Where Alltests.java is defined as follows:

public class Alltests 

extends TestSuite {The public     

 static Test suite () {return
 new Testsuitebuilder ( Alltests.class)
 . Includeallpackagesunderhere (). Build 
 ();
 }     

Use Testsuitebuilder to indicate that all testcase defined in the package and its child packages are part of the final testsuite (tests that need to be run).

Alltests.java can generally be applied to most test projects without modification, and if you have special needs, you can use Android.test.suitebuilder classes to define which testcase needs to be included in the final Test suite.

Apidemosapplicationtests test introduces the basic usage of applicationtestcase.

The following figure is the inheritance relationship for Applicationtestcase:

Applicationtestcase is primarily used to test the application class, provides basic support for the application class lifecycle approach, and can support some dependency injection To help construct the environment for testing application.

Application lifecycle support, each application run follows a certain sequence (lifecycle approach), Applicationtestcase provides the following methods to support testing of the application lifecycle method:

The application OnCreate method is executed only after the test case calls CreateApplication (), giving you the opportunity to make adjustments to the test framework before invoking the application OnCreate method.

When the test case ends, the teardown method of the test case is invoked, and then the application OnDestroy () method is invoked to stop and destroy application.

Dependency injection Each application relies on the context object that runs it, and the Android test framework allows you to inject a mock or orphaned session object to achieve real unit testing if you just want to do a generic test , your application method will be injected into a fully functional context object, and you can use SetContext to inject the context object of your defined mock, setcontext must be called before createapplication. The test framework provides such as Mockcontext, Renamingdelegatingcontext, contextwrapper to help you construct a context object for testing Applicaton.

The code for this example is as follows:

public class Apidemosapplicationtests     
 extends applicationtestcase<apidemosapplication> {     

 public Apidemosapplicationtests () {     
 super (Apidemosapplication.class);     
 }     

 @Override 
 protected void SetUp () throws Exception {     
 super.setup ();     
 }     

 @SmallTest public 
 void Testpreconditions () {     
 }     

 /**    
 * Test basic Startup/shutdown of application    
 */ 
 @MediumTest public 
 void Testsimplecreate () {     
 createapplication ();     
 }     

}

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.