1.Android JUnit Runner (using Androidstudio)

Source: Internet
Author: User
Tags custom name

First, Android junit Runner introduction 1.Android junit Runner

1) is a test runner for running Junit3 and JUNIT4 Android test packages
2) Replace the instrumentation test Runner (an older test runner)
3) Support Instrumentation Test Runner all features, but extended
4) maintains all instrumentation Test Runner command formats

2. Inheritance Relationship
Java.lang.Object    android.app.Instrumentation        android.support.test.runner.MonitoringInstrumentation            Android.support.test.runner.AndroidJUnitRunner
Ii. Instrument Test Commands 1. Command format:
AM instrument [-r] [-e <NAME> <value>] [-P <file>] [-W]                     [--user <user_id>|  Current]                     [--no-window-animation] [--abi <abi>] <COMPONENT>//COMPONENT: Package name/ Run Runner
    • Examples of running all test case commands:
adb shell AM Instrument-W Com.testdemo.test/android.support.test.runner.androidjunitrunner
2. Test commands

Parameters

Description

-R

Print the original results (if you do not add test results information will be very small)
Generate raw output performance metrics using [-e perf True]

-E

Setting parameters
Common forms for Test runner are:
[-E [,...]]
Writing profiling data to a file
Note: You need to first create a trace file

-W

Wait for the test to complete (this is a required parameter)

–user |
Current

Specifies that the user runs, if not specified, to run with the current user
–no-window-animation Turn off window animations (equivalent to opening animations) and run

–abi

Select the supported CPU architecture by starting the process with the selected ABI
can be used (adb shell Getprop

3. Example (using Android Studio):

1) Search in GitHub android-testing select Googlesamples/android-testing to go in, and then download the. zip Archive Package
2) Unzip after download, all dependencies are under Android-testing-master\ui\espresso\basicsamplebundled\libs path
3) File–new–new Project (wait for project creation to complete) –app–java–[packagename right-]–new–java class
Note: After opening Java there will be two options, the above is the APK code, the following is the test code, we write test cases are generally written in the test code package
4) Copy all files except Android-support-v4.jar in the Android-testing-master\ui\espresso\basicsamplebundled\libs directory to the Libs folder of the created project
5) Select Copy to go to all files right--add as library-in the pop-up prompt window select App-ok
6) After adding it, you can switch to Android view, view it in Gradle Scripts/build.gradle (Module:app), and comment out the first line * (the line of the comment indicates that it will compile all the jar packages in the libs below. Because we've already added it manually so we don't need it, the code looks like this:

Dependencies {//compile Filetree (include: [' *.jar '], dir: ' Libs ')Testcompile ' junit:junit:4.12 'Compile' com.android.support:appcompat-v7:23.1.1 'Compile' com.android.support:design:23.1.1 'Compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/espresso-contrib-2.2-release-no-dep.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/espresso-core-2.2-release-no-dep.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/espresso-idling-resource-2.2-release-no-dep.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/espresso-intents-2.2-release-no-dep.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/espresso-web-2.2-release-no-dep.jar ') compile files (' c:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/ Exposed-instrumentation-api-publish-0.3-release-no-dep.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/guava-18.0.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/hamcrest-core-1.3.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/hamcrest-integration-1.3.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/hamcrest-library-1.3.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/javawriter-2.1.1.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/javax.annotation-api-1.2.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/javax.inject-1.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/jsr305-2.0.1.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/junit-4.12.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/rules-0.3-release-no-dep.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/runner-0.3-release-no-dep.jar ') compile files (' C:/users/xuzhonghui/androidstudioprojects/runnerdemo/libs/uiautomator-v18-2.1.1-release-no-dep.jar ')}

5) Click on the navigation bar Run-edit configurations...-Click the + sign-android Tests in the upper left corner of the popup screen, configure reference



    • Description
      A.name can be set by itself
      B.target option Please set according to the actual situation, my side is to take the simulator to debug

6) Add the debugger to the defalutconfig of the Gradle scripts/build.gradle (module:app) file, and the added code appears as follows:

Android {compilesdkversion23buildtoolsversion"23.0.2"Defaultconfig {ApplicationID"Com.example.xuzhonghui.runnerdemo"minsdkversion15targetsdkversion23Versioncode1 Versionname "1.0"//the content in the quotation marks is the runner you just createdTestinstrumentationrunner"Android.support.test.runner.AndroidJUnitRunner"} buildtypes {release {minifyenabledfalseproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro '         }     } }

7) At this point, the basic configuration is complete, then write the test code in the class you just created, the code is as follows:

 PackageCom.example.xuzhonghui.runnerdemo;Importandroid.app.Instrumentation;ImportAndroid.support.test.InstrumentationRegistry;ImportANDROID.SUPPORT.TEST.RUNNER.ANDROIDJUNIT4;ImportAndroid.support.test.uiautomator.UiDevice;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;//the next line must be to specify which runner runs@RunWith (ANDROIDJUNIT4.class)//Test Cases Public classRunnertest {//Initializing the deviceUidevice device; @Before Public voidsetip{Device=uidevice.getinstance (Instrumentationregistry.getinstrumentation ())}//use Case 1@Test Public voidtest1 () {device.pressmenu (); }    //use case 2@Test Public voidtest2 () {device.pressback (); }    //use case 3@Test Public voidtest3 () {device.pressmenu (); }    //use case 4@Test Public voidtest4 () {device.pressback (); }}

8) After the code is written, you need to reconfigure the runner, the configuration diagram is as follows:



    • Configuration Description:
      A. Select the test class in Test and add the class that you just wrote the test code to, which is the red position.
      B.show Device Chooser Dialog A dialog box that pops up a selection target device

9) then run it

    • Precautions:

The first time I follow the above steps to run, there is a problem, the specific principle is not clear, after the understanding of the fix, problems and solutions please refer here: http://blog.csdn.net/qq_26967883/article/details/ 50477071 First: excution faild for Task ': App:transformresourceswithmergejavaresfordebug '

4.Instrument command Simple to use
#列出手机中所有已安装的包, the interface can be seen just using Android Studio installed package adb shell pm List instrumentation #该命令中不打印详细日志,The parameters after-e are just the <packageName>.<className> of creating a use case in Android Studioadb shell AM instrument-w-eclassCom.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.androidjunitrunner# compared to the previous command, just one more<-r>parameter, is to let him output verbose run log, equivalent to eclipes debug uiautomator when output log adb shell AM instrument-w-r-EclassCom.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.androidjunitrunner# One more than the last one.The-p+file parameter is intended to save the run log to a. trace file. Use this command to first use the command "Touch Runner.trace" in the device in ADB shell modeCreate a custom name for the. trace file adb shell am instrument-w-r-P/DATA/LOCAL/TMP/RUNNER.TRACE-EclassCom.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/ Android.support.test.runner.AndroidJUnitRunner
Third, Android JUnit runner filter with example 1. Filter

Filter parameters

Description

-E Testfile Run the use case specified in the file
-E Package Run all the use cases in this package
-e size [Small|medium|large] Run the use case for annotation small/mediumtest/largetest
-E annotation To run a use case for a specified comment
-E Notannotation Run a use case that does not contain a specified comment
-E Numshards Splitting a use case into different slices
-E Shardindex To run the use case for the specified tile ID

2. Example

3. Example

1) First modify the above using the Android studio to create the use case code, add a few test methods, I add 10 here, is the key information, after the completion of the first run, remember to push the package to the phone

#列出手机中所有已安装的包找到com. example.xuzhonghui.runnerdemo.test/Android.support.test.runner.AndroidJUnitRunner is the package that we created and installed into the phone using Android. adb shell pm List instrumentation# Run all use cases , no log information C:\Users\xuzhonghui&GT;ADB Shell am instrument-w com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.androidjunitrunner# Run all use cases, with log information C:\Users\xuzhonghui&GT;ADB Shell am Instrument-w-R com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.androidjunitrunner# Run the use case for the specified class (Specify Class=package name. Class name) C:\Users\xuzhonghui&GT;ADB Shell am instrument-w-r-rclassCom.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.androidjunitrunner# Run one of the use cases in the specified class (after the specified class, add a"#用例名") C:\Users\xuzhonghui&GT;ADB Shell am instrument-w-r-rclassCom.example.xuzhonghui.runnerdemo.runnertest#test3 com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.androidjunitrunner# Run multiple classes of use cases, before running the first one in Android studio to create a more class and copy a few use cases into the convenience (only two classes are shown here, Multiple classes in the same vein, add a comma) C:\Users\xuzhonghui&GT;ADB Shell am instrument-w-r-rclassCom.example.xuzhonghui.runnerdemo.runnertest,com.example.xuzhonghui.runnerdemo.runnertest2 Com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.androidjunitrunner

Create a. txt document with the contents of the document, and then push the document to the phone using the command "ADB push f:\testfile.txt/data/local/tmp"

-w-r-E testfile/data/local/tmp/testfile.txt com.example.xuzhonghui.runnerdemo.test/Package Com.example.xuzhonghui.runnerdemo com.example.xuzhonghui.runnerdemo.test/ Android.support.test.runner.AndroidJUnitRunner

Modify the use cases previously created in Android Studio and add a few comments, such as (remember to run it on your phone after you've modified it):

-w-r-E size smallcom.example.xuzhonghui.runnerdemo.test/-w-r-E size Medium com.example.xuzhonghui.runnerdemo.test/-w-r-E size Large com.example.xuzhonghui.runnerdemo.test/ Android.support.test.runner.AndroidJUnitRunner

Modify the use case in Androidstudio, the import of the Smalltext comment in the previous step to find the annotation method of writing, and then according to this self to write the annotation method, write a good method such as:


After writing, follow the previous step to add comments to the use case, modify the use case PostScript to run first

#运行指定自定义注释SmokeAnnotation的用例 (note here that the annotation followed by a comment that was written by itself < package name >.< class name >-w-r-  E Annotation com.example.xuzhonghui.runnerdemo.SmokeAnnotation com.example.xuzhonghui.runnerdemo.test/-W- R-  e notannotation com.example.xuzhonghui.runnerdemo.SmokeAnnotation com.example.xuzhonghui.runnerdemo.test/ Android.support.test.runner.AndroidJUnitRunner
Four, Android JUnit runner parameters

Mainly used in white box testing, rather than uiautomator, interested in in-depth study

Parameters Description
-E Log True Run only in log mode
This option loads and iterates through the classes and methods of all tests, but bypasses the actual test execution. Instrument commands for quick access to information to perform tests
-E Coverage true Generate Code Coverage
This requires a Emma build. By default, the code coverage results file will be saved in the/data//coverage.ec file unless there is a coveragefile flag
-E Coveragefile Specify code coverage File save path
-E Coveragefile/sdcard/myfile.ec
-E Listener Specify one or more runlisteners to observe the test run
-E Listener Com.foo.listener,com.foo.listener2
-E Timeout_mse Setting the time-out (in milliseconds) will be applied to each test
-E Timeout_mec 5000
-E Disableanlytics True Disable Google Analytics

    • Parameters can also be specified in the Androidmainfest file

How to use the listener:

In the actual use of the process, simply add the above code in the Androidmanifect manifest file, the actual use of reference


    • Parameters passed through the shell command will overwrite the manifest specified parameters

1.Android JUnit Runner (using Androidstudio)

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.