[Eclipse插件开发指南]2.8 插件测试编写

Eclipse是持续发展的一个产品,当构建商业插件的时候, 测试是很必要的,这样才能解决一些复杂的原因导致的产品问题。如果目标是开发以及发布了一个一次性的插件,那么手动测试就足够了;但是,自动测试随着产品的不断的发展能够更好的改进产品。

2.8.1 测试准备

在测试创建Favorites视图之前,你必须修改Favorites插件的清单,这样测试插件才能访问一些需要的类。双击plugin.xml文件打开插件清单编辑器,然后打开Runtime页面(图2-11)。在Exported Packages部分,点击Add…,选择com.qualityeclipse.favorites.views包,然后,选择File > Save保存所有改变。

接着,添加一个适合的存取器,这样插件就能验证视图的内容。在FavoritesView类中,加入以下方法:

/** * For testing purposes only. * @return the table viewer in the Favorites view */ public TableViewer getFavoritesViewer() { return viewer; }

附语:你可以在插件清单编辑器中Package Visibility部分的Runtime页面去限制你导出包的可见性。你可以选择将测试放在包中,这样没有包需要被导出了。

2.8.2 插件测试项目的创建

用一个类似2.2部分:创建插件项目的过程,按以下方式创建新的插件项目:

● 命名项目为com.qualityeclipse.favorites.test

● 修改插件类为com.qualityeclipse.favorites.test.FavoritesTestPlugin

● 不要在复选框Create a plug-in using one of these templates

项目创建后,用插件清单编辑器Dependencies页面(图2-10)去加入以下需要的插件以及保存改变

● com.qualityeclipse.favorites

● org.junit

2.8.3 创建一个测试插件

当项目已经创建以及插件清单修改后,是时间去对Favorites插件做一个简单的测试了。目标是测试显示的Favorites视图,验证其中的内容,然后隐藏它。

package com.qualityeclipse.favorites.test; import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.eclipse.core.runtime.Platform; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.PlatformUI; import com.qualityeclipse.favorites.views.FavoritesView; /** * The class <code>FavoritesViewTest</code> contains tests * for the class {@link * com.qualityeclipse.favorites.views.FavoritesView}. * * @pattern JUnit Test Case * @generatedBy CodePro Studio */ public class FavoritesViewTest extends TestCase { private static final String VIEW_ID = "com.qualityeclipse.favorites.views.FavoritesView"; /** * The object that is being tested. * * @see com.qualityeclipse.favorites.views.FavoritesView */ private FavoritesView testView; /** * Construct new test instance. * * @param name the test name */ public FavoritesViewTest(String name) { super(name); } /** * Perform pre-test initialization. * * @throws Exception * * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); // Initialize the test fixture for each test // that is run. waitForJobs(); testView = (FavoritesView) PlatformUI .getWorkbench() .getActiveWorkbenchWindow() .getActivePage() .showView(VIEW_ID); // Delay for 3 seconds so that // the Favorites view can be seen. waitForJobs(); delay(3000); // Add additional setup code here. } /** * Perform post-test cleanup. * * @throws Exception * * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); // Dispose of test fixture. waitForJobs(); PlatformUI .getWorkbench() .getActiveWorkbenchWindow() .getActivePage() .hideView(testView); // Add additional teardown code here. } /** * Run the view test. */ public void testView() { TableViewer viewer = testView.getFavoritesViewer(); Object[] expectedContent = new Object[] { "One", "Two", "Three" }; Object[] expectedLabels = new String[] { "One", "Two", "Three" }; // Assert valid content. IStructuredContentProvider contentProvider = (IStructuredContentProvider) viewer.getContentProvider(); assertEquals(expectedContent, contentProvider.getElements(viewer.getInput())); // Assert valid labels. ITableLabelProvider labelProvider = (ITableLabelProvider) viewer.getLabelProvider(); for (int i = 0; i < expectedLabels.length; i++) assertEquals(expectedLabels[i], labelProvider.getColumnText(expectedContent[i], 1)); } /** * Process UI input but do not return for the * specified time interval. * * @param waitTimeMillis the number of milliseconds */ private void delay(long waitTimeMillis) { Display display = Display.getCurrent(); // If this is the UI thread, // then process input. if (display != null) { long endTimeMillis = System.currentTimeMillis() + waitTimeMillis; while (System.currentTimeMillis() < endTimeMillis) { if (!display.readAndDispatch()) display.sleep(); } display.update(); } // Otherwise, perform a simple sleep. else { try { Thread.sleep(waitTimeMillis); } catch (InterruptedException e) { // Ignored. } } } /** * Wait until all background tasks are complete. */ public void waitForJobs() { while (Platform.getJobManager().currentJob() != null) delay(1000); } /** * Assert that the two arrays are equal. * Throw an AssertionException if they are not. * * @param expected first array * @param actual second array */ private void assertEquals(Object[] expected, Object[] actual) { if (expected == null) { if (actual == null) return; throw new AssertionFailedError( "expected is null, but actual is not"); } else { if (actual == null) throw new AssertionFailedError( "actual is null, but expected is not"); } assertEquals( "expected.length " + expected.length + ", but actual.length " + actual.length, expected.length, actual.length); for (int i = 0; i < actual.length; i++) assertEquals( "expected[" + i + "] is not equal to actual[" + i + "]", expected[i], actual[i]); } }

2.8.4 运行一个测试插件

新建了插件类后下一步去配置以及执行这个测试。与创建运行配置相识(2.6.1部分),右键点击FavoritesViewTest,选择Run As > JUnit Plug-in Test。这样就自动构建了测试配置,你可以执行这个测试。Runtime工作台显示出来了,Favorites视图打开了,然后关闭Runtime工作台。JUnit视图表明了你的测试是成功的,Favorites视图的内容也显示正确了(图2-27)。

图2-27 测试配置向导页

image

右击FavoritesViewTest,选择Run As > Run...打开配置(Configuration)向导页(图2-28)。这里可以指定是执行单个测试还是同时执行所有项目中的测试。

图2-28 测试配置向导页

image

2.8.5 卸载Favorites

在开发工作空间中按照以下步骤去删除Favorites插件:

1. 关闭Favorites视图

2. 关闭Eclipse

3. 在插件目录下删除com.quality.favorites_1.0.0.jar。

4. 重启Eclipse。如果重启的时候出现了错误(图2-29),那就是在第二步的时候还有Favorites视图没有关闭完。

图2-29 重启Eclipse时候的问题弹出窗口

image

5. 打开Show View弹出窗口(图2-18)验证Favorites视图是不是已经不再了。同时验证Quality Eclipse分类是不是也没有了。

本文系eclipselight.org(日食之光)原创文章,转载请注明出处。
固定链接:http://www.eclipselight.org/eclipse-plugin-tutorial/742/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值