zoukankan      html  css  js  c++  java
  • Robotium编写测试用例如何模拟Junit4的BeforeClass和AfterClass方法2

    在上一遍笔记博客中本以为只能在Setup和TearDown中做条件判断来实现Junit4的@BeforeClass和@AfterClass功能。今天查看SDK时发现其实是有现成的方法来实现这个功能的。

    方法就是把编写的测试用例从继承自ActivityInstrumentationTestCase2改成继承成SingleLaunchActivityTestCase。

    为什么这样做就可以了呢?请先看SingleLaunchActivityTestCase的Class Overview:

    SingleLaunchActivityTestCase
    Class Overview
    If you would like to test a single activity with an InstrumentationTestCase, this provides some of the boiler plate 
    to launch and finish the activity in setUp() and tearDown(). This launches the activity only once for the entire 
    class instead of doing it in every setup / teardown call.

    大概意思就是说如果测试用例是继承自SingleLaunchActivityTestCase的话,setTup()和tearDown()会只运行一次而不是像ActivityInstrumentationTestCase2每调用一次函数都会运行一次。而这不刚好解决了我们的问题了吗!

    代码如下:

    1. package com.example.android.notepad.test;  
    2.   
    3. import com.robotium.solo.Solo;  
    4.   
    5. import android.test.ActivityInstrumentationTestCase2;  
    6. import android.test.SingleLaunchActivityTestCase;  
    7. import android.app.Activity;  
    8.   
    9. @SuppressWarnings("rawtypes")  
    10. public class TCCreateNote2 extends SingleLaunchActivityTestCase{  
    11. //public class TCCreateNote2 extends ActivityInstrumentationTestCase2{  
    12.   
    13.     private static Solo solo = null;  
    14.     public Activity activity;  
    15.       
    16.     private static final int NUMBER_TOTAL_CASES = 2;  
    17.     private static int run = 0;  
    18.       
    19.     private static Class<?> launchActivityClass;  
    20.   
    21.     //对应re-sign.jar生成出来的信息框里的两个值  
    22.     private static String mainActiviy = "com.example.android.notepad.NotesList";  
    23.     private static String packageName = "com.example.android.notepad";  
    24.   
    25.     static {  
    26.   
    27.         try {  
    28.   
    29.             launchActivityClass = Class.forName(mainActiviy);  
    30.   
    31.         } catch (ClassNotFoundException e) {  
    32.   
    33.             throw new RuntimeException(e);  
    34.   
    35.         }  
    36.   
    37.     }  
    38.       
    39.       
    40.     @SuppressWarnings("unchecked")  
    41.     public TCCreateNote2() {  
    42.         super(packageName, launchActivityClass);  
    43.     }  
    44.   
    45.       
    46.     @Override  
    47.     public void setUp() throws Exception {  
    48.         //setUp() is run before a test case is started.   
    49.         //This is where the solo object is created.  
    50.         super.setUp();   
    51.         //The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated  
    52.         // which would lead to soto to re-instantiated to be null if it's not set as static  
    53.         //if(solo == null) {  
    54.         TCCreateNote2.solo = new Solo(getInstrumentation(), getActivity());  
    55.         //}  
    56.     }  
    57.       
    58.     @Override  
    59.     public void tearDown() throws Exception {  
    60.         //Check whether it's the last case executed.  
    61.         run += countTestCases();  
    62.         if(run >= NUMBER_TOTAL_CASES) {  
    63.             solo.finishOpenedActivities();  
    64.         }  
    65.     }  
    66.   
    67.     public void testAddNoteCNTitle() throws Exception {  
    68.           
    69.         solo.clickOnMenuItem("Add note");  
    70.         solo.enterText(0, "中文标签笔记");  
    71.         solo.clickOnMenuItem("Save");  
    72.         solo.clickInList(0);  
    73.         solo.clearEditText(0);  
    74.         solo.enterText(0, "Text 1");  
    75.         solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor");  
    76.         solo.clickOnMenuItem("Save");  
    77.         solo.assertCurrentActivity("Expected NotesList Activity", "NotesList");  
    78.           
    79.         solo.clickLongOnText("中文标签笔记");  
    80.         solo.clickOnText("Delete");  
    81.     }  
    82.       
    83.       
    84.     public void testAddNoteEngTitle() throws Exception {  
    85.         solo.clickOnMenuItem("Add note");  
    86.         solo.enterText(0, "English Title Note");  
    87.         solo.clickOnMenuItem("Save");  
    88.         solo.clickInList(0);  
    89.         solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor");  
    90.         solo.clearEditText(0);  
    91.         solo.enterText(0, "Text 1");  
    92.         solo.clickOnMenuItem("Save");  
    93.         solo.assertCurrentActivity("Expected NotesList Activity", "NotesList");  
    94.           
    95.         solo.clickLongOnText("English Title Note");  
    96.         solo.clickOnText("Delete");
  • 相关阅读:
    Sql server2008如何导入Excel文件数据?
    oracle和sql server中,取前10条数据语法的区别
    如何将两个字段合成一个字段显示(oracle和sqlserver的区别)
    php递归注意事项
    PHP实现执行定时任务
    商城怎么使用ajax?
    添加新权限管理
    PHP判断一个JSON对象是否含有某一个属性的方法
    centos 中查找文件、目录、内容
    phpStorm中如何不让其自动添加封闭大括号?
  • 原文地址:https://www.cnblogs.com/TestingOn/p/3980949.html
Copyright © 2011-2022 走看看