====== Android Unit Test introduction ======
JUnit is an open source Java Unit Testing Framework, and the test suite for Android is based on JUnit 3 (not fully compatible with JUnit 4), and the JUNIT4 can be tested with ordinary junit using a simple understanding. It is recommended to use the Android JUnit test framework for efficient and comprehensive testing.
====== Android Unit Test Framework UML ======
{{:d olphin_news:share:androidjunituml.png?300 |}}
Andriod Project Unit Test ====== under ====== eclipse
Using Androidjunit to do unit testing, you can roughly divide the following steps:
-New Android Test Project
-New JUnit test case
-New JUnit test suite
-Run JUnit Test
===== New Android Test Project =====
This part is done on the basis of importing Shell_en_aglie in Eclipse, first make sure that you have successfully imported the Shell_en_agile project (as well as other projects). New Android Test Project method:
-In Eclipse File->new->other->android->androidtestproject, enter the test project name, such as input dolphin_junit_test.
-Next select the project to be tested, such as selecting Test Dolphinbroweren, this step is complete.
Now let's open androidmanifest.xml and look at it.
<code>
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
<!--the package name has been modified and will automatically be mobi.mgeek.tunnybrower.test--> according to the project being tested.
Package= "Com.dolphin.browser.core.test"
Android:versioncode= "1"
Android:versionname= "1.0" >
<USES-SDK android:minsdkversion= "7"/>
<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Mobi.mgeek.TunnyBrower"/>
<application
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name" >
<uses-library android:name= "Android.test.runner"/>
</application>
</manifest>
</code>
As you can see, some content has been automatically generated, such as uses-library and instrumentation have been automatically generated, here need to pay attention to the package name, because we chose to test the project as Dolphinbroweren, So here's the automatic for US targetpackage= "Mobi.mgeek.TunnyBrowser", and for the test project package name package= "Mobi.mgeek.TunnyBrowe.test", The package name here can be modified as needed, but targetpackage= "Com.dolphin.browser.core" is the package name of the project being tested and cannot be modified.
===== New Junit Test case =====
New JUnit Test Case method:
-File->new->other->junit->junit Test case in eclipse
-In the JUnit Test Case dialog box that pops up, select New JUnit 3 Test,superclass Select Androidtestcase, and select Class under Test to be tested, enter the name Class name, and click Next.
-Select the method that needs to be tested, here according to the actual situation to choose the method that needs to be tested, click Finish.
===== New Junit Test Suite =====
To create a new Junit Test Suite method:
-File->new->other->junit->junit Test Suite in eclipse
-In the JUnit Test Case dialog box that pops up, select the class that you want to test.
-Select the method that needs to be tested, here according to the actual situation to choose the method that needs to be tested, enter the name and click Finish.
-Write Testsuite code
-After writing the testsuite, you need to add a instrumentation to the androidmanifest, and the added androidmanifest becomes
<code>
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.dolphin.browser.core.test"
Android:versioncode= "1"
Android:versionname= "1.0" >
<USES-SDK android:minsdkversion= "7"/>
<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Com.dolphin.browser.core"/>
<!--add Testsuite items--
<instrumentation
Android:name= "Com.dolphin.browser.core.test.Testall"
Android:targetpackage= "Com.dolphin.browser.core"/>
<application
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name" >
<uses-library android:name= "Android.test.runner"/>
</application>
</manifest>
</code>
Testsuite code after the completion of the code, the use of inheritance instrumentationtestrunner, mainly through the Addtestsuite to add the test class, here I have only one Test class, when there are more than one test class, you can add them all in.
<code java>
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;
}
}
</code>
===== Running Junit =====
can refer to http://blog.csdn.net/xianming01/article/details/7463066, because the test project is large, in eclipse caused by the graphical interface to run JUnit, so we can use the ADB The shell am mode runs the generated JUnit apk to test. The specific methods are:
-Install the tested APK into the phone
-Install the JUnit program-generated apk into your phone
-Enter the ADB shell PM list instrumentation, which displays information similar to the following, showing two instruementation, the first test case, and the second Test suite.
Instrumentation:com.dolphin.browser.junittest/android.test.instrumentationtestrunner (target= Mobi.mgeek.TunnyBrowser) \ \
Instrumentation:com.dolphin.browser.junittest/.testall. Testall (Target=mobi.mgeek.tunnybroer)
As described in the link, you can enter ADB shell am instrument-w com.dolphin.browser.junittest/ Android.test.InstrumentationTestRunner execute a single test case, or you can
Enter ADB shell am instrument-w com.dolphin.browser.junittest/.testall. Testall to execute test suite.
===== Precautions =====
-When creating a project, pay attention to the relationship between package and Targetpackage.
-Since I am testing dolphinbrowseren and cannot compile in eclipse, I used the build test apk and then installed it on my phone and tested it using the ADB shell am mode.
-How to write test methods, do not explain here, you need to pay attention to the test method is an asynchronous method, such as HTTP, you need to use thread synchronization to wait for the completion of the asynchronous task.
-We can also write an activity, draw a simple interface, click the button to trigger the unit test, or the interface can be unit tested.
====== gradle Environment for Android Unit test method one ======
This section takes the Development/samples/notepad of AOSP as an example, the unit test code under Notepad works in its tests folder.
Step together:
* Create a new folder Testjunit (name any), copy the Development/samples/notepad item to the inside.
* Now the file structure is testjunit inside is the Notepad project, the project is SRC, res, tests, androidmanifest and so on.
* New three files in Testjunit, Build.gradle, Common.gradle, Settings.gradle.
Build.gradle content is as follows:
<code>
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.+ '
}
}
}
}
</code>
Common.gradle content is as follows:
<code>
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 ')
}
}
</code>
Here instrumenttest can also be written in a different 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 the way to specify the directory separately.
Settings.gradle content is as follows:
<code>
Include ': NotePad '
</code>
* Create a new build.gradle in the Notepad folder with the following contents:
<code>
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"
}
}
}
</code>
Note: The above four gradle files I copied from the Shell_en_agile, referring to its structure and modified.
* Compile
In Testjunit run Gradle--daemon assembledebug will generate the Notepad project APK, run Gradle--daemon assembletest will generate the unit test project APK.
* Run
Install test apk, run adb shell am instrument-w com.dolphin.browser.junittest/android.test.instrumentationtestrunner.
====== gradle Environment to do Android unit test configuration Method two ======
Implementation of the work, in order to make the tests project with the test project git commit message is not interference, you can separate unit test management, and the project is relatively independent, tests project will not affect the Notepad project. On the basis of method one, proceed to the following operation.
Step together:
* NotePad in the tests cut out to NotePad parallel position, now the file structure is testjunit there are two folders, NotePad and tests, NotePad inside only SRC, res and so on.
* CD NotePad, link the test project via a soft link: ln-s. /tests tests.
Compiler
Compile the Gradle--deamon installtest under Testjunit, and after compiling, you can see if the corresponding test file class file is generated under build, and verify that the compilation is not a problem.
Run
Refer to the test engineering tests, the specific command can refer to the Alltests.java in Aosp/development/samples/apidemos/tests.
To run all suites found in this apk:adb shell am instrument-w com.dolphin.browser.junittest/android.test.instrumentation Testrunner \ \
To run just this suite from the command LINE:ADB Shell am INSTRUMENT-W-E class Com.dolphin.browser.junittest.TestAll Co M.dolphin.browser.junittest/android.test.instrumentationtestrunner \ \
To run an individual test case:adb shell am instrument-w-E com.dolphin.browser.junittest.xxxtest com.dolphin.browser.ju Nittest/android.test.instrumentationtestrunner \ \
To run an individual TESTCASE:ADB shell am instrument-w-E class Com.dolphin.browser.junittest.news.test.NewsRequestWrap Pertest com.dolphin.browser.junittest/android.test.instrumentationtestrunner \ \
Android Unit test in Gradle compilation environment