Perform android unit testing in the gradle compiling environment

Source: Internet
Author: User

Perform android unit testing in the gradle compiling environment
===== Android unit test Introduction ======
JUnit is an open-source java unit testing framework. The android test suite is based on JUnit 3 (not fully compatible with JUnit 4). Junit4 only needs a simple understanding, you can use junit for testing. We recommend that you use the Junit test framework of android for efficient and comprehensive testing.
===== Android unit test framework UML ======
{: Dolphin_news: share: androidjunituml.png? 300 | }}


===== Andriod project unit test in eclipse ======
You can perform unit tests using AndroidJunit in the following steps:
-Create an Android Test Project
-Create a Junit test case
-Create Junit test suite
-Run Junit test.
===== Create an Android Test Project ====
This part is completed by importing shell_en_aglie in eclipse. First, make sure that you have successfully imported the shell_en_agile project (the same is true for other projects ). Method for creating an Android Test Project:
-In eclipse, choose File> New> Other> Android> AndroidTestProject and enter the test project name, for example, dolphin_junit_test.
-Next, select the project to be tested. For example, select test DolphinBrowerEn.
Now let's open AndroidManifest. xml and take a look.



Package = "com. doldolphin. browser. core. test"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

Android: name = "android. test. InstrumentationTestRunner"
Android: targetPackage = "mobi. mgeek. TunnyBrower"/>
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">




We can see that some content has been automatically generated, such as uses-library and instrumentation. Note the package name here, because we have selected the project to be tested as DolphinBrowerEn, so the targetPackage = "mobi. mgeek. tunnyBrowser ", and for the test project package name package =" mobi. mgeek. tunnyBrowe. test ", the package name can be modified as needed, but targetPackage =" com. doldolphin. browser. core is the package name of the project to be tested and cannot be modified.
===== Create a new Junit Test Case ====
Method for creating Junit Test Case:
-In eclipse, choose File> New> Other> Junit Test Case.
-In the displayed Junit Test Case dialog box, select New Junit 3 test, Superclass select AndroidTestCase, select Class under test to be tested, enter the Name Class Name, and click Next.
-Select the method to be tested. Here, select the method to be tested and click Finish.
===== Create a new Junit Test Suite ====
Method for creating Junit Test Suite:
-In eclipse, choose File> New> Other> Junit Test Suite.
-In the pop-up Junit Test Case dialog box, select the class to be tested.
-Select the method to be tested. Select the method to be tested based on the actual situation, enter the Name, and click Finish.
-Write the TestSuite code.
-After writing TestSuite, you need to add an instrumentation to AndroidManifest. The added AndroidManifest becomes


Package = "com. doldolphin. browser. core. test"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

Android: name = "android. test. InstrumentationTestRunner"
Android: targetPackage = "com. doldolphin. browser. core"/>

Android: name = "com. doldolphin. browser. core. test. Testall"
Android: targetPackage = "com. doldolphin. browser. core"/>
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">




After the TestSuite code is complete, the Code uses the inherited InstrumentationTestRunner, mainly to add Test classes through addTestSuite. Here I only have one test class. When there are multiple test classes, you can add them all.

public class TestAll extends InstrumentationTestRunner{


private static final String TAG = "TestAll";


public TestSuite getAllTests(){
Log.i(TAG, "Junit test, testsuite");
TestSuite suite = new InstrumentationTestSuite(this);
suite.addTestSuite(NewsRequestWrapperTest.class);
return suite;
}
}

===== Run Junit ====
You can refer to the http://blog.csdn.net/xianming01/article/details/7463066, because the project to be tested is large, in eclipse, resulting in the failure to run Junit through the graphical interface, so we can use adb shell am to run the generated junit apk for testing. The specific method is:
-Install the tested apk on your mobile phone
-Install the apk generated by the junit program on your mobile phone.
-Input adb shell pm list instrumentation. The following information is displayed: instruementation, first test case, and second test suite.


Instrumentation: com. doldolphin. browser. junittest/android. test. InstrumentationTestRunner (target = mobi. mgeek. TunnyBrowser )\\
Instrumentation: com. doldolphin. browser. junittest/. testall. TestAll (target = mobi. mgeek. TunnyBroer)

According to the instructions in the Link, you can enter adb shell am instrument-w com. doldolphin. browser. junittest/android. test. InstrumentationTestRunner to execute a single test case, or
Enter adb shell am instrument-w com. doldolphin. browser. junittest/. testall. TestAll to execute test suite.


===== Note ======
-When creating a project, pay attention to the relationship between package and targetPackage.
-Since I tested DolphinBrowserEn and it cannot be compiled in eclipse, I used to generate the test apk, install it on my mobile phone, and use adb shell am for testing.
-Do not describe how to write the test method here. When the test method is asynchronous, such as http, thread synchronization is required to wait for the completion of the asynchronous task.
-You can also write an activity to draw a simple interface. click the button to trigger the unit test. You can also perform the unit test on the interface.




===== Method 1 of android unit test in gradle Environment
This section takes the development/samples/NotePad in AOSP as an example. The unit test code of the NotePad project is in its tests folder.
Steps:
* Create a new folder named testJunit, and copy the development/samples/NotePad project to it.
* The file structure is testJunit, which contains the NotePad project, src, res, tests, and AndroidManifest.
* Create three new files in testJunit: build. gradle, common. gradle, settings. gradle.
The content of build. gradle is as follows:

def gradle = project.getGradle()
gradle.beforeProject { project ->


if (project.file('build.gradle').exists()) {
project.buildscript {
repositories {
maven {
name = "Baina Maven Proxy"
url = "http://mirrors.baina.com:8080/archiva/repository/internal"
}
}


dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
}
}

The content of common. gradle is as follows:

tasks.withType(JavaCompile) {
options.fork = true
options.forkOptions.setMemoryMaximumSize("4096m")
options.forkOptions.setMemoryInitialSize("1024m")
options.encoding = "UTF-8"
}


android {
compileSdkVersion 17
buildToolsVersion "18.0.1"


sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest {
java.srcDirs = ['tests/src']
resources.srcDirs = ['tests/src']
aidl.srcDirs = ['tests/src']
renderscript.srcDirs = ['tests/src']
res.srcDirs = ['tests/res']
assets.srcDirs = ['tests/assets']
}


//instrumentTest.setRoot('tests')
}
}

Here, instrumentTest can also be written in another way.
InstrumentTest. setRoots ('tests '). In this case, you need to change the src in NotePad to java. In order to run in eclipse, we recommend that you specify the directory separately.


The settings. gradle content is as follows:

include ':NotePad'



* Create build. gradle In the NotePad folder. The content is as follows:

apply plugin: 'android'
apply from: "$project.rootDir/common.gradle"


android {
compileSdkVersion 16


buildTypes {
release {
runProguard true
proguardFile 'proguard.cfg'
}


debug {
runProguard true
proguardFile 'proguard-debug.cfg'
}
}

defaultConfig {
testPackageName "com.example.android.notepad.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
}

}

Note: I copied the above four gradle files from shell_en_agile. Refer to its structure and modify it.
* Compile
Run gradle -- daemon assembleDebug in testJunit to generate the apk of the NotePad project. Run gradle -- daemon assembleTest to generate the apk of the unit test project.
* Run
Install test apk and run adb shell am instrument-w com. doldolphin. browser. junittest/android. test. InstrumentationTestRunner.


===== Configuration method 2 for android unit test in gradle environment =====
In implementation, in order to ensure that the tests project does not interfere with the git commit message of the project to be tested, the unit test can be managed independently, which is relatively independent from the project to be tested, the tests project does not affect the NotePad project. Perform the following operations on the basis of method 1.
Steps:
* Cut the tests in NotePad and place it in the parallel position of NotePad. Now the file structure is testJunit, which contains two folders: NotePad and tests. Only src and res are left in NotePad.
* Cd NotePad: connect the test project to ln-s ../tests through soft links.
* Compilation:
Compile gradle -- deamon installTest under testJunit. After compilation, you can check whether the class file of the single test file is generated under build to confirm that the compilation is correct.
* Run:
Refer to the test project tests. For detailed commands, refer to AllTests. java in AOSP/development/samples/ApiDemos/tests.


To run all suites found in this apk: adb shell am instrument-w com. doldolphin. browser. junittest/android. test. InstrumentationTestRunner \\
To run just this suite from the command line: adb shell am instrument-w-e class com. doldolphin. browser. junittest. testAll com. doldolphin. browser. junittest/android. test. instrumentationTestRunner \\
To run an individual test case: adb shell am instrument-w-e com. doldolphin. browser. junittest. xxxtest com. doldolphin. browser. junittest/android. test. InstrumentationTestRunner \\
To run an individual testcase: adb shell am instrument-w-e class com. doldolphin. browser. junittest. news. test. newsRequestWrapperTest com. doldolphin. browser. junittest/android. test. instrumentationTestRunner \\


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.