Android开发----自动化测试

最近看了看Android的自动化测试,收集了不少资料。自动化测试可理解为测试过程自动化和测试结果分析自动化。

Android下自动化测试的方法与工具有Android自带的Monkey,有第三方开发的Robotium,还有TMTS(Taobao Mobile Test Studio)框架等。下面一一介绍。


一、Monkey工具

Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流(如按键输入、触摸屏输入、手势输入等),实现对正在开发的应用程序进行压力测试。Monkey测试是一种为了测试软件的稳定性、健壮性的快速有效的方法。

The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.(官方介绍)

http://developer.android.com/tools/help/monkey.html


Monkey的特征:

1、测试的对象仅为应用程序包,有一定的局限性。
2、 Monky测试使用的事件流数据流是随机的,不能进行自定义。
3、可对MonkeyTest的对象,事件数量,类型,频率等进行设置。


Monkey的基本用法:

基本语法如下:
$ adb shell monkey [options]
如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包。下面是一个更为典型的命令行示例,它启动指定的应用程序,并向其发送500个伪随机事件:
$ adb shell monkey -p your.package.name -v 500


Monkey的测试步骤:

1、通过eclipse启动一个Android的emulator

2、在cmd命令行中输入:adb devices查看设备连接情况

3、在有设备连接的前提下,在命令行中输入:adb shell进入shell界面

4、查看data/data(输入 ls  data/data)文件夹下的应用程序包。注:我们能测试的应用程序包都在这个目录下面

5、如以com.android.calculator2作为对象进行MonkeyTest

则命令行输入:#monkey-p com.android.calculator2 -v 500



可以看到,屏幕的输入、点击等事件都是随机的。Monkey主要用来进行压力测试。


二、Robotium测试工具


Robotium工具,可以在有app源码或apk的情况下进行自动化黑盒测试

Robotium是一个基于Android应用程序的自动化黑盒测试工具。它简化了测试用例的编写,并且能够编写出功能强大,健壮性很强的黑盒测试用例。运用Robotium,测试人员能够编写测试用例,系统测试,验收测试方案等。同时Robotium还能够跨越多个Android的Activity,进行测试。

Robotium对Activity,Dialog,Toast,Menu都是支持的。

Robotium是开源的:http://code.google.com/p/robotium/downloads/list

测试Robotium可以下载官方的例子ExampleTestProject_v4.2.zip 


直接导入zip文件,里面有两个工程NotePadNotePadTest,右键点击NotePadTest文件,run as Android JunitTest即可。



运行结果


可以看到,模拟器按照预先在NotePadTest项目中写好的流程进行自动操作,当测试全部成功时,绿条变满。

关于Robotium测试的具体用法,和Junit有很多相似之处。详细的可参照robotium-solo-4.2-javadoc.jar中的帮助文档以及上面的例子来写。


下面给出NotePadTest例子,带注释。

  1. public class NotePadTest extends ActivityInstrumentationTestCase2{  
  2.   
  3.     private Solo solo;//测试主类   
  4.   
  5.     public NotePadTest()  
  6.         super(NotesList.class);//指定要测试的类   
  7.   
  8.      
  9.   
  10.     //启动   
  11.     @Override  
  12.     public void setUp() throws Exception  
  13.         //setUp() is run before test case is started.    
  14.         //This is where the solo object is created.   
  15.         solo new Solo(getInstrumentation(), getActivity());  
  16.      
  17.       
  18.     //结束   
  19.     @Override  
  20.     public void tearDown() throws Exception  
  21.         //tearDown() is run after test case has finished.    
  22.         //finishOpenedActivities() will finish all the activities that have been opened during the test execution.   
  23.         solo.finishOpenedActivities();  
  24.      
  25.   
  26.     //测试用例: 添加笔记   
  27.     public void testAddNote() throws Exception  
  28.         solo.clickOnMenuItem("Add note");  
  29.         //Assert that NoteEditor activity is opened   
  30.         solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor");   
  31.         //In text field 0, enter Note 1   
  32.         solo.enterText(0, "Note 1");  
  33.         solo.goBack();   
  34.         //Clicks on menu item   
  35.         solo.clickOnMenuItem("Add note");  
  36.         //In text field 0, type Note 2   
  37.         solo.typeText(0, "Note 2");  
  38.         //Go back to first activity   
  39.         solo.goBack();   
  40.         //Takes screenshot and saves it in "/sdcard/Robotium-Screenshots/".   
  41.         solo.takeScreenshot();  
  42.         boolean expected true;  
  43.         boolean actual solo.searchText("Note 1") && solo.searchText("Note 2");  
  44.         //Assert that Note Note are found   
  45.         assertEquals("Note and/or Note are not found", expected, actual);   
  46.   
  47.      
  48.       
  49.     //测试用例: 编辑笔记   
  50.     public void testEditNote() throws Exception  
  51.         // Click on the second list line   
  52.         solo.clickInList(2);   
  53.         //Hides the soft keyboard   
  54.         solo.hideSoftKeyboard();  
  55.         // Change orientation of activity   
  56.         solo.setActivityOrientation(Solo.LANDSCAPE);  
  57.         // Change title   
  58.         solo.clickOnMenuItem("Edit title");  
  59.         //In first text field (0), add test   
  60.         solo.enterText(0, test");    
  61.         solo.goBack();  
  62.         boolean expected true;  
  63.         // (Regexp) case insensitive   
  64.         boolean actual solo.waitForText("(?i).*?note test");   
  65.         //Assert that Note test is found   
  66.         assertEquals("Note test is not found", expected, actual);   
  67.   
  68.      
  69.       
  70.     //测试用例: 移除笔记   
  71.     public void testRemoveNote() throws Exception  
  72.         //(Regexp) case insensitive/text that contains "test"   
  73.         solo.clickOnText("(?i).*?test.*");  
  74.         //Delete Note test   
  75.         solo.clickOnMenuItem("Delete");  
  76.         //Note test Note should not be found   
  77.         boolean expected false;     
  78.         boolean actual solo.searchText("Note test");  
  79.         //Assert that Note test is not found   
  80.         assertEquals("Note Test is found", expected, actual);    
  81.         solo.clickLongOnText("Note 2");  
  82.         //Clicks on Delete in the context menu   
  83.         solo.clickOnText("Delete");    
  84.         //Will wait 100 milliseconds for the text: "Note 2"   
  85.         actual solo.waitForText("Note 2", 1, 100);  
  86.         //Assert that Note is not found   
  87.         assertEquals("Note is found", expected, actual);    
  88.      
  89.  
public class NotePadTest extends ActivityInstrumentationTestCase2{

        private Solo solo;//测试主类

        public NotePadTest() {
                super(NotesList.class);//指定要测试的类

        }

        //启动
        @Override
        public void setUp() throws Exception {
                //setUp() is run before a test case is started. 
                //This is where the solo object is created.
                solo = new Solo(getInstrumentation(), getActivity());
        }
        
        //结束
        @Override
        public void tearDown() throws Exception {
                //tearDown() is run after a test case has finished. 
                //finishOpenedActivities() will finish all the activities that have been opened during the test execution.
                solo.finishOpenedActivities();
        }

        //测试用例: 添加笔记
        public void testAddNote() throws Exception {
                solo.clickOnMenuItem("Add note");
                //Assert that NoteEditor activity is opened
                solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); 
                //In text field 0, enter Note 1
                solo.enterText(0, "Note 1");
                solo.goBack(); 
                //Clicks on menu item
                solo.clickOnMenuItem("Add note");
                //In text field 0, type Note 2
                solo.typeText(0, "Note 2");
                //Go back to first activity
                solo.goBack(); 
                //Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/".
                solo.takeScreenshot();
                boolean expected = true;
                boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2");
                //Assert that Note 1 & Note 2 are found
                assertEquals("Note 1 and/or Note 2 are not found", expected, actual); 

        }
        
        //测试用例: 编辑笔记
        public void testEditNote() throws Exception {
                // Click on the second list line
                solo.clickInList(2); 
                //Hides the soft keyboard
                solo.hideSoftKeyboard();
                // Change orientation of activity
                solo.setActivityOrientation(Solo.LANDSCAPE);
                // Change title
                solo.clickOnMenuItem("Edit title");
                //In first text field (0), add test
                solo.enterText(0, " test");  
                solo.goBack();
                boolean expected = true;
                // (Regexp) case insensitive
                boolean actual = solo.waitForText("(?i).*?note 1 test"); 
                //Assert that Note 1 test is found
                assertEquals("Note 1 test is not found", expected, actual); 

        }
        
        //测试用例: 移除笔记
        public void testRemoveNote() throws Exception {
                //(Regexp) case insensitive/text that contains "test"
                solo.clickOnText("(?i).*?test.*");
                //Delete Note 1 test
                solo.clickOnMenuItem("Delete");
                //Note 1 test & Note 2 should not be found
                boolean expected = false;   
                boolean actual = solo.searchText("Note 1 test");
                //Assert that Note 1 test is not found
                assertEquals("Note 1 Test is found", expected, actual);  
                solo.clickLongOnText("Note 2");
                //Clicks on Delete in the context menu
                solo.clickOnText("Delete");  
                //Will wait 100 milliseconds for the text: "Note 2"
                actual = solo.waitForText("Note 2", 1, 100);
                //Assert that Note 2 is not found
                assertEquals("Note 2 is found", expected, actual);  
        }
}

更多资料:http://blog.51cto.com/zt/301


三、TMTS(Taobao Mobile Test Studio)测试框架


Robotium目前的缺点也很明显,无法对WebView进行操作,这对大量使用WebView的淘宝Android客户端来说无疑是很大的限制。

而且Robotium提供的API是面向过程的,测试代码的可扩展性差。

我们需要一个面向对象的,可对WebView进行操作的自动化测试框架,这就催生了TMTS(Taobao Mobile Test Studio)框架。

TMTS立项时还试图着重解决另一个问题,就是Instrumentation框架下testapp和app运行在一个进程中 ,app crash会导致testapp一并crash。


相关文章: http://www.taobaotesting.com/blogs

源码:http://code.taobao.org/p/TMTS/src/


原创文章,转载请注明出处:http://blog.csdn.net/xn4545945

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值