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

    本文来源于:http://blog.csdn.net/zhubaitian/article/details/39296753

    在上一遍笔记博客中本以为只能在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 import android.test.ActivityInstrumentationTestCase2;
     5 import android.test.SingleLaunchActivityTestCase;
     6 import android.app.Activity;
     7 
     8 @SuppressWarnings("rawtypes")
     9 public class TCCreateNote2 extends SingleLaunchActivityTestCase{
    10     //public class TCCreateNote2 extends ActivityInstrumentationTestCase2{
    11     private static Solo solo = null;
    12     public Activity activity;
    13     private static final int NUMBER_TOTAL_CASES = 2;
    14     private static int run = 0;
    15     
    16     private static Class<?> launchActivityClass;
    17     //对应re-sign.jar生成出来的信息框里的两个值
    18     private static String mainActiviy = "com.example.android.notepad.NotesList";
    19     private static String packageName = "com.example.android.notepad";
    20 
    21     static {
    22         try {
    23             launchActivityClass = Class.forName(mainActiviy);
    24         } catch (ClassNotFoundException e) {
    25             throw new RuntimeException(e);
    26         }
    27     }
    28     
    29     @SuppressWarnings("unchecked")
    30     public TCCreateNote2() {
    31         super(packageName, launchActivityClass);
    32     }
    33     
    34     @Override
    35     public void setUp() throws Exception {
    36         //setUp() is run before a test case is started. 
    37         //This is where the solo object is created.
    38         super.setUp(); 
    39         //The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated
    40         // which would lead to soto to re-instantiated to be null if it's not set as static
    41         //if(solo == null) {
    42         TCCreateNote2.solo = new Solo(getInstrumentation(), getActivity());
    43         //}
    44     }
    45     
    46     @Override
    47     public void tearDown() throws Exception {
    48         //Check whether it's the last case executed.
    49         run += countTestCases();
    50         if(run >= NUMBER_TOTAL_CASES) {
    51             solo.finishOpenedActivities();
    52         }
    53     }
    54 
    55     public void testAddNoteCNTitle() throws Exception {
    56         solo.clickOnMenuItem("Add note");
    57         solo.enterText(0, "中文标签笔记");
    58         solo.clickOnMenuItem("Save");
    59         solo.clickInList(0);
    60         solo.clearEditText(0);
    61         solo.enterText(0, "Text 1");
    62         solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor");
    63         solo.clickOnMenuItem("Save");
    64         solo.assertCurrentActivity("Expected NotesList Activity", "NotesList");
    65         solo.clickLongOnText("中文标签笔记");
    66         solo.clickOnText("Delete");
    67     }
    68     
    69     public void testAddNoteEngTitle() throws Exception {
    70         solo.clickOnMenuItem("Add note");
    71         solo.enterText(0, "English Title Note");
    72         solo.clickOnMenuItem("Save");
    73         solo.clickInList(0);
    74         solo.assertCurrentActivity("Expected EditTest Activity", "NoteEditor");
    75         solo.clearEditText(0);
    76         solo.enterText(0, "Text 1");
    77         solo.clickOnMenuItem("Save");
    78         solo.assertCurrentActivity("Expected NotesList Activity", "NotesList");
    79         solo.clickLongOnText("English Title Note");
    80         solo.clickOnText("Delete");
    81     }
    82 }
  • 相关阅读:
    warning MSB3245: 未能解析此引用。未能找到程序集“CemeteryBLL”。请检查磁盘上是否存在该程序集。 如果您的代码需要此引用,则可能出现编译错误。
    C#MVC中创建多模块web应用程序
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
    未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf385
    WebHost failed to process a request.Memory gates checking failed because the free memory (140656640 bytes) is less than 5% of total memory
    wamp安装后打开默认网页显示dir,图标红点
    数字转为大写,钱转为大写格式
    SpringCloud
    Spring Boot
    双重校验锁 --使用volatile和两次判空校验
  • 原文地址:https://www.cnblogs.com/dtest/p/4164360.html
Copyright © 2011-2022 走看看