[转]Android Test Auto Test Mu…

来源:互联网 发布:淘宝转让店铺 编辑:程序博客网 时间:2024/05/14 17:30

在Android SDK中“Resources”-“Tutorials”下有“NotepadTutorial”和“ActivityTesting”两个项目,一个示例是指导你如何快速开发一个Android小程序,一个是指导你如何对项目进行测试,两个项目都适合在入门的时候好好学习。

   其中的“ActivityTesting”是对“Samples”-“Spinner”项目进行测试,其中包含了UI测试、状态破坏和状态恢复测试。这个项目只有一个Activity,测试起来也不麻烦,细心阅读文档就可以完成。但是一个程序只有一个Activity应该是很难遇见的吧,那么应该对多活动(MultiActivities)的程序进行测试呢?
   其实我这也是随便整整,大家随便看看。

在查看SDK关于测试的章节后,有疑问如下:
测试Activity、Service、Provider都是自动化的,那么我们如何控制运行过程?
如何在界面模拟操作,如点击按钮,输入文字内容等等。


   新建一个项目,项目名为Login,包名为com.vruc.android.login,程序名为Login,活动名为AuthenticateActivity;同时添加一个项目名为LoginTest,包名为com.vruc.android.login.test,程序名为LoginTest的测试项目。

   完整的Login项目:
  1.更改main.xml文件名为login.xml,更改代码为下:
Xml代码 
<?xml version="1.0"encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:orientation="vertical"android:layout_width="fill_parent" 
   android:layout_height="fill_parent"> 
   <EditTextandroid:id="@+id/username_field" 
       android:layout_height="wrap_content"android:layout_width="match_parent"></EditText> 
   <EditTextandroid:id="@+id/password_field" 
       android:layout_height="wrap_content"android:layout_width="match_parent"></EditText> 
   <Button android:text="Login"android:id="@+id/login_button" 
       android:layout_width="wrap_content"android:layout_height="wrap_content"></Button> 
</LinearLayout> 

  2.打开AuthenticateActivity.java文件,为“@+id/login_button”添加点击事件,具体代码就是向WelcomeActivity传递当前“@+id/username_field”中的输入文字并结束当前activity,具体代码为下:
Java代码 
public class AuthenticateActivity extends Activity{  
    
   @Override 
    public voidonCreate(Bundle savedInstanceState){  
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.login);  
       Button login = (Button)findViewById(R.id.login_button);  
       login.setOnClickListener(new OnClickListener(){  
           @Override 
           public void onClick(View v){  
               Intent i = newIntent(AuthenticateActivity.this,WelcomeActivity.class);  
               i.putExtra(ACCOUNT_SERVICE,((EditText)findViewById(R.id.username_field)).getText().toString());  
               startActivity(i);  
               finish();  
            
       });  
    

 
  3.在layout目录下添加新文件welcome.xml,更改代码为下:
Xml代码 
<?xml version="1.0"encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   android:orientation="vertical"android:layout_width="fill_parent" 
   android:layout_height="fill_parent"> 
   <TextViewandroid:id="@+id/welcome_message" 
       android:layout_width="wrap_content"android:layout_height="wrap_content" 
       android:textSize="15pt"></TextView> 
</LinearLayout> 

  4.添加新的WelcomeActivity.java文件并在AndroidMainifest.xml中注册,重写onCreate事件,具体代码就是为“@+id/welcome_message”赋值,从LoginActivity中传递过来的“@+id/username_field”的值,具体代码为下:
Java代码 
public class WelcomeActivity extends Activity{  
   @Override 
    public voidonCreate(Bundle savedInstanceState){  
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.welcome);  
       Intent i =this.getIntent();  
       ((TextView)findViewById(R.id.welcome_message)).setText(i.getStringExtra(ACCOUNT_SERVICE));  
   

  现在可以运行一下Login项目,可以发现填写在“@+id/username_field”中的文字在点击“@+id/login_button”按钮后出现在了WelcomeActivity中。

   完整的LoginTest项目
  1.添加LoginTest.java文件,继承类为android.test.InstrumentationTestCase
   2.完整LoginTest.java中测试代码:
Java代码 
public static final String TEST_USERNAME ="TEST_USERNAME";  
public static final String TEST_PASSWORD ="TEST_PASSWORD";  
 
public void testUserLogin(){  
 
    //注册最开始的活动并运行  
   Instrumentation instrumentation =getInstrumentation();  
   ActivityMonitor monitor =instrumentation.addMonitor(  
           AuthenticateActivity.class.getName(), null,false);  
 
    //运行活动  
    Intentintent = newIntent(Intent.ACTION_MAIN);  
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
   intent.setClassName(instrumentation.getTargetContext(),AuthenticateActivity.class.getName());  
   instrumentation.startActivitySync(intent);  
      
    //等待Authenticate活动开始  
    ActivitycurrentActivity =getInstrumentation().waitForMonitorWithTimeout(monitor,5);  
   assertTrue(currentActivity !=null);  
 
    //自动输入预定的用户名  
    ViewcurrentView =currentActivity.findViewById(com.vruc.android.login.R.id.username_field);  
   assertTrue(currentView !=null);  
   TouchUtils.clickView(this,currentView);  
   instrumentation.sendStringSync(TEST_USERNAME);  
 
    //自动输入预定的密码  
    currentView=currentActivity.findViewById(com.vruc.android.login.R.id.password_field);  
   assertTrue(currentView !=null);  
   TouchUtils.clickView(this,currentView);  
   instrumentation.sendStringSync(TEST_PASSWORD);  
 
    //移除当前活动监视,注册新的活动监视,要在还没有按下按钮前准备  
   instrumentation.removeMonitor(monitor);  
    monitor =instrumentation.addMonitor(WelcomeActivity.class.getName(), null,false);  
 
    //自动点击登陆按钮  
    currentView=currentActivity.findViewById(com.vruc.android.login.R.id.login_button);  
   assertTrue(currentView !=null);  
   TouchUtils.clickView(this,currentView);  
 
    //等待Welcome活动开始  
   currentActivity =getInstrumentation().waitForMonitorWithTimeout(monitor,5);  
 
    currentView=currentActivity.findViewById(com.vruc.android.login.R.id.welcome_message);  
   assertTrue(currentView !=null);  
   assertEquals(TEST_USERNAME, ((TextView)currentView).getText().toString());  
 

 运行测试程序后可以发现testUserLogin顺利通过,也可以在模拟器查看具体的运行过程,就像你新手操作模拟器一般。

原创粉丝点击