Robotium编写测试用例如何模拟Junit4的BeforeClass和AfterClass方法1 - 条件判断法

发布时间:2020-07-31 07:14:02 作者:zhukev
来源:网络 阅读:552

Robotium的测试类ActivityInstrumentationTestCase2是继承于Junit3的TestCase类,所以并没有提供Junit4的特性.如网上总结说的

那么有时我们并不想每次开始/完成一个case的时候都做一些重复的动作,也就是要实现Junit4的@beforeclass和@afterclass,该怎么办呢?
以SDK自带的Notepad测试用例作为例子,假如现在我们需要实现两个测试用例
根据实例提供的代码,在setup里面会初始化solo而在teardown里面会关闭所有打开的activities,也就是说每执行一个case都会重新初始化一次solo和关闭所有的activities:
	@Override 	public void setUp() throws Exception { 		//setUp() is run before a test case is started.  		//This is where the solo object is created. 		super.setUp();  		this.activity = this.getActivity();  		this.solo = new Solo(getInstrumentation(), getActivity()); 	} 	 	@Override 	public void tearDown() throws Exception { 		//tearDown() is run after a test case has finished.  		//finishOpenedActivities() will finish all the activities that have been opened during the test execution. 		solo.finishOpenedActivities(); 	} 
但事实上我们在这个脚本只是去创建两个Note,并不需要每执行完一个case都要去初始化solo和关闭所有activities。google后没有发现有现成的取代@beforeclass和@aferclass的方法。
以下本人的实现方法
<pre name="code" class="java">package com.example.android.notepad.test;  import com.robotium.solo.Solo;  import android.test.ActivityInstrumentationTestCase2; import android.app.Activity;  @SuppressWarnings("rawtypes") public class TCCreateNote extends ActivityInstrumentationTestCase2{  	private static Solo solo = null; 	public Activity activity; 	 <span style="white-space:pre">	</span>private static final int NUMBER_TOTAL_CASES = 2; 	private static int run = 0; 	 	private static Class<?> launchActivityClass;  	//对应re-sign.jar生成出来的信息框里的两个值 	private static String mainActiviy = "com.example.android.notepad.NotesList"; 	private static String packageName = "com.example.android.notepad";  	static {  		try {  			launchActivityClass = Class.forName(mainActiviy);  		} catch (ClassNotFoundException e) {  			throw new RuntimeException(e);  		}  	} 	 	 	@SuppressWarnings("unchecked") 	public TCCreateNote() { 		super(packageName, launchActivityClass); 	}  	 	@Override 	public void setUp() throws Exception { 		//setUp() is run before a test case is started.  		//This is where the solo object is created. 		super.setUp();  <span style="white-space:pre">		</span>//The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated 		// which would lead to soto to re-instantiated to be null if it's not set as static 		if(solo == null) { 			TCCreateNote.solo = new Solo(getInstrumentation(), getActivity()); 		} 	} 	 	@Override 	public void tearDown() throws Exception { 		//Check whether it's the last case executed. 		run += countTestCases(); 		if(run >= NUMBER_TOTAL_CASES) { 			solo.finishOpenedActivities(); 		} 	}  	public void testAddNoteCNTitle() throws Exception { 		 		solo.clickOnMenuItem("Add note"); 		solo.enterText(0, "中文标签笔记"); 		solo.clickOnMenuItem("Save"); 		solo.clickInList(0); 		solo.clearEditText(0); 		solo.enterText(0, "Text 1"); 		solo.clickOnMenuItem("Save"); 		solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); 		 		solo.clickLongOnText("中文标签笔记"); 		solo.clickOnText("Delete"); 	} 	 	 	public void testAddNoteEngTitle() throws Exception { 		solo.clickOnMenuItem("Add note"); 		solo.enterText(0, "English Title Note"); 		solo.clickOnMenuItem("Save"); 		solo.clickInList(0); 		solo.clearEditText(0); 		solo.enterText(0, "Text 1"); 		solo.clickOnMenuItem("Save"); 		solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); 		 		solo.clickLongOnText("English Title Note"); 		solo.clickOnText("Delete"); 	} }


  

推荐阅读:
  1. ET·ci — 全自动软件测试调度(持续集成)平台
  2. Postgresql中DDL的双引号问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

自动化测试 android unit test

上一篇:Docker 仓库 Harbor

下一篇:人生苦短,我用Python(目录)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》