第六章 测试架构规划及测试用例组织(上)

------手机自动化测试之Robotium

   在编写完具体的测试用例后,我们就要组织一下测试用例,这样方便进行回归测试。结合我们的自动化测试的传统,我们继续采取原来的方法来组织我们的测试用例。

6.1 测试架构规划

    由于测试用例执行的时候是在手机上执行的,所以类似于Web的把测试数据存放到Xml的方法是不可用的,因为在测试用例运行的时候找不到电脑上存放的xml文件。当然也有手机上数据驱动的方法,见:http://www.cnblogs.com/freeliver54/archive/2011/08/06/2129343.html。测试数据过多的话,不建议放到手机上,所以我们将测试数据写到具体的测试用例中。

   现在我们的架构就变成了下面这样的:

(1)Src下创建package:com.zhongchou.CommonFunctions:在这个package下存放我们常用的操作类。

(2)Src下创建page:com.zhongchou.TestCases:在这个package下存放具体的测试用例文件。

(3)Src下创建page:com.zhongchou.TestSuites:在这个package下存放组织用例的TestSuite

(4)在工程下创建文件夹test-output:存放从手机上获取的测试用例报告。

6.2 测试用例组织

    在确定好具体的架构后,我们就要编写具体的测试用例相关的类以及测试用例等。

6.2.1 公用类的创建

在我们/src/com/zhongchou/CommonFunctions下创建所有测试用例都要用到的类库,如:CommonFunctions.java,文件中存放通用的类,功能模块等。

代码示例:

package com.zhongchou.CommonFunctions;

 

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import com.robotium.solo.Solo;

import junit.framework.Assert;

 

public class CommonFunctions extends Assert{

 

      

       private Solo solo;

      

      

       public CommonFunctions(Solo so)

       {

              solo=so;

       }

      

       public void clickbyid(String id)

       {

                       

              View view=null;

              view=solo.getView(id);        

              solo.clickOnView(view);

              solo.sleep(3000);

       }

      

       public String gettextbyid(String id)

       {

             

              TextView view;

              view=(TextView)solo.getView(id);

              return (String) view.getText();

       }    

      

       public void login(String name,String psd)

       {

             

              this.clickbyid("com.subject.zhongchou:id/user_head");

              solo.sleep(1000);

              solo.clickOnText("马上登录");

              solo.sleep(1000);

              solo.clickOnText("普通账号登录");

              solo.sleep(1000);

              EditText editname=solo.getEditText(0);

              solo.enterText(editname, name);

              EditText editpsd=solo.getEditText(1);

              solo.enterText(editpsd, psd);

              solo.sleep(2000);

              this.clickbyid("com.subject.zhongchou:id/go_numberlogin");

              solo.sleep(3000);          

       }

      

       public void logout()

       {

             

              this.clickbyid("com.subject.zhongchou:id/user_head");

              solo.sleep(1000);

              solo.clickOnText("设置");

              solo.sleep(1000);

              solo.clickOnText("退出登录");

              solo.sleep(1000);

              solo.clickOnButton("确定");

              solo.sleep(3000);

             

       }    

}

其中包含了登录和退出功能模块和一些儿常用操作,注释很清楚,就不做过多讲解。

6.2.2 具体的测试用例编写

    在我们/src/com/zhongchou/TestCases下创建具体的测试用例,由于我们没有使用数据驱动,所以具体的测试用例数据要放到这个文件中。下面我们以众筹App的登录测试用例为例 ,展示一下代码,其实具体的编写,我们在前面的章节已经讲过了。

登录录测试用例示例:

package com.zhongchou.TestCases;

 

import static org.junit.Assert.*;

import java.io.File;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import android.test.ActivityInstrumentationTestCase2;

import android.view.View;

import com.robotium.solo.Solo;

 

import com.zhongchou.CommonFunctions.CommonFunctions;

 

public class LoginTest extends  ActivityInstrumentationTestCase2 {

       private Solo solo; 

       public CommonFunctions comfun=null;

      

       private static final String TARGET_PACKAGE_ID="com.subject.zhongchou";

       private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.subject.zhongchou.activity.SplashActivity";

       //声明一个Class类型的变量,用于ActivityInstrumentationTestCase2加载启动被测程序

       private static Class launcherActivityClass;

       //声明一个标签用于日志的输出控制,便于调试

       final String TAG="Test->N00";

       //静态加载auncherActivityClass也就是被测程序主类

       static{

              try {

                     launcherActivityClass = Class

                                   .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);

              } catch (ClassNotFoundException e) {

                     throw new RuntimeException(e);

              }

       }

       //构造函数,传入TARGET_PACKAGE_ID,launcherActivityClass即可

       public LoginTest()

       {

              super(TARGET_PACKAGE_ID,launcherActivityClass);

       }

       @Before

       public void setUp() throws Exception {    

             

              solo = new Solo(getInstrumentation(), getActivity());

       }

 

       @After

       public void tearDown() throws Exception {

              solo.finishOpenedActivities();

       }

 

       @Test

       public void test_login() throws Exception {

             

              comfun=new CommonFunctions(solo);

              solo.sleep(1000);

              comfun.login("183****8905", "a0****0");

              assertEquals("潜龙9527",comfun.gettextbyid("com.subject.zhongchou:id/leveltext"));

              comfun.clickbyid("com.subject.zhongchou:id/user_head");

              solo.sleep(2000);

              comfun.logout();

              assertTrue(solo.searchText("登录"));

                           

             

       }

 

}

   在这个测试用例中,我们调用了通用测试类中的登录和退出功能,以很少的代码,完成具体的测试用例。这也是我们封装具体测试功能模块的原因,使测试用例简单,便于维护。

6.2.3 组织测试用例

   当我们完成对被测对象的测试用例覆盖的时候,就需要用TestSuite来组织测试用例,方便回归测试。具体方法是在/src/com/zhongchou/TestSuites下创建Junit Test Suite文件,如图6.2.3所示。

 

6.2.3创建TestSuite文件

  我们的TestSuite文件名为AllTests.java,具体代码如下:

package com.zhongchou.TestSuites;

 

import junit.framework.Test;

import junit.framework.TestSuite;

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

import org.junit.runners.Suite.SuiteClasses;

 

import com.zhongchou.TestCases.LoginTest;//引用测试用例

 

@RunWith(Suite.class)

@SuiteClasses({})

public class AllTests {

      

       public static Test suite()

       {

              TestSuite suite=new TestSuite("Dragon Test");

              suite.addTestSuite(LoginTest.class);//添加测试用例

             

              return suite;

       }

      

       public static void main(String args[])

       {

              junit.textui.TestRunner.run(suite());

       }

 

}

我们只需要把需要集中运行的测试用例先import进来,然后使用函数suite.addTestSuite()添加到Suite中,然后运行TestSuite文件即可运行所有的测试用例。

原文地址:https://www.cnblogs.com/eagleking0318/p/6520833.html