[Android Learning 11] Android Dev Unit test

Source: Internet
Author: User


Online about the Android Unit Test text posts abound, after reading can solve the problem is good, can not solve the problem that is a waste of time and experience. Most articles come from this article:http://rexstjohn.com/unit-testing-with-android-studio/


Since being put into the Android Studio IDE, I've basically been on the move. Only the Android unit tests in as (Android Studio) are discussed here.


Developing an Android Application unit test this programmer's self-cultivation and basic ethical testing is essential. The unit tests are divided into two pieces, one for unit tests that rely on the Android API (platform) and one for unit tests that do not rely on the Android API.


1. Unit tests that do not rely on ANDROIDAPI

This piece is just like the unit tests that normally develop javase,ee applications, the test framework needs to be chosen on its own. When you create an Android app module from as, the test library JUnit dependency is added by default in the Build.gradle file.


If you are using unit tests that do not rely on ANDROIDAPI in as, one of the top words:


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7A/02/wKiom1agjcCSriQ1AABZef3y67U911.png "title=" Uniit_ Test_1.png "alt=" Wkiom1agjccsriq1aabzef3y67u911.png "width=" 340 "height=" 436 "border=" 0 "hspace=" 0 "vspace=" 0 "style= "WIDTH:340PX;HEIGHT:436PX;"/>650) this.width=650; "Src=" http://s2.51cto.com/wyfs02/M01/7A/01/ Wkiol1agjf3s6xy1aabetvcju0i158.png "title=" Unit_test_2.png "alt=" Wkiol1agjf3s6xy1aabetvcju0i158.png "width=" 340 " height= "447" border= "0" hspace= "0" vspace= "0" style= "width:340px;height:447px;"/>

Figure A figure II


Two shows that after the test artifact of build variants is set to unit tests, the test source directory presents the idea-style green directory. This is how you can run a test class or test suite, which is easier to explain.


2. Unit tests that rely on the Android API


Configuring Android unit tests in 2.1 as


Android's JUNIT3 framework provides Android's unit testing framework, and the use of unit testing in Android requires a dependency on the specific classes under the Android.test package. Here's how to configure the Android unit test in as:


650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/7A/03/wKiom1agjt7R8Qb7AAEg-StiPA8246.png " Title= "Android_tester.png" alt= "Wkiom1agjt7r8qb7aaeg-stipa8246.png"/>


Might

2.2 Android Unit Test example


2.2.1 A unit test class:

package secondriver.heathfood.test;import android.test.instrumentationtestcase;import  com.orhanobut.logger.logger;import secondriver.heathfood.app;import  secondriver.heathfood.logic.tiangou;import secondriver.heathfood.pojo.foodclassresult;import  secondriver.heathfood.pojo.fooddetail;import secondriver.heathfood.pojo.fooddetailresult;/** *  author : secondriver * created : 2016/1/20 */public class  testtiangou extends instrumentationtestcase {    public tiangou  Tiangou;    public void setup ()  {         tiangou = new tiangou (App.resttemplate ());    }     PUBLIC VOID TEST_QUERYFOODCLASSBYPARENTID1 ()  {         foodclassresult result = tiangou.queryfoodclassbypArentid (0);         asserttrue (Result.isstatus ());     }}



For the unit test base class for Android see:

Http://www.oschina.net/question/54100_27061?fromerr=NydwIvQR .

http://www.uml.org.cn/mobiledev/201306074.asp

Note: The test class for the Android unit test follows the JUNIT3.

2.2.2 Performing unit tests

Before performing a unit test, the test artifact of the build variant is set to the value shown in figure one.


After performing Android tester, output:


650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/7A/03/wKiom1agkCewIdOgAAA9lXBzGLg080.png " Title= "Android_test.png" alt= "Wkiom1agkcewidogaaa9lxbzglg080.png"/>

Figure Four

I learned from the above that the network access rights are not granted.


2.2.3 as in Android Unit test console

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7A/03/wKiom1agkKiTGT5pAABnZo5LjPQ465.png "title=" Android_tester_console.png "alt=" Wkiom1agkkitgt5paabnzo5ljpq465.png "/>

Figure Five



In the online search on the Android unit test case, compared to clutter, so this article to be collated, Android unit test how to do when it is another topic, you need to refer to the specific classes provided in the Android.test package.



The Android test class package is created directly under the main code directory, which is a good way to see the following Android test code directory structure:


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7A/03/wKiom1agl5rAgMotAABZK25bMQk778.png "title=" Android_tester_directory_structure.png "alt=" Wkiom1agl5ragmotaabzk25bmqk778.png "/>

Figure Six

This article is from the "Red Horse Red" blog, please be sure to keep this source http://aiilive.blog.51cto.com/1925756/1737254

[Android Learning 11] Android Dev Unit test

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.