• 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");
  • 相关阅读:
    Python 三级菜单
    linux 下按文件类型删除
    linux 做内网端口映射
    ss
    fio
    libXtst.so.6 is needed by teamviewer-12.0.76279-0.i686
    copy 浅复制 与深复制
    Git 使用方法
    关于 爬虫使用 urllib.urlopen 提交默认 User-Agent值
    Python 官方模块文档
  • 原文地址:https://www.cnblogs.com/TestingOn/p/3980949.html
Copyright © 2020-2023  润新知