在swtbot的单元测试中需要设置Run to Product的参数制,本来想通过参数配置,不用每次手动设置.运行研究结果.首先上图:
可以发现:
1.首先线程调用的Test的测试工作台WorkbenchTestable
/** * Initializes the workbench testable with the display and workbench, * and notifies all listeners that the tests can be run. * * @param display the display * @param workbench the workbench */ public void init(Display display, IWorkbench workbench) { Assert.isNotNull(display); Assert.isNotNull(workbench); this.display = display; this.workbench = workbench; if (getTestHarness() != null) { // don't use a job, since tests often wait for all jobs to complete before proceeding Runnable runnable = new Runnable() { public void run() { // Some tests (notably the startup performance tests) do not want to wait for early startup. // Allow this to be disabled by specifying the system property: org.eclipse.ui.testsWaitForEarlyStartup=false // For details, see bug 94129 [Workbench] Performance test regression caused by workbench harness change if (!"false".equalsIgnoreCase(System.getProperty(PlatformUI.PLUGIN_ID + ".testsWaitForEarlyStartup"))) { //$NON-NLS-1$ //$NON-NLS-2$ waitForEarlyStartup(); } getTestHarness().runTests(); } }; new Thread(runnable, "WorkbenchTestable").start(); //$NON-NLS-1$ } }
2.然后调用UITestApplication类的runTests方法.
/* * (non-Javadoc) * @see org.eclipse.ui.testing.ITestHarness#runTests() */ public void runTests() { fTestableObject.testingStarting(); RemotePluginTestRunner.main(Platform.getCommandLineArgs()); fTestableObject.testingFinished(); }
Platform.getCommandLineArgs()的参数值如下:
[-version, 3, -port, 60193, -testLoaderClass, org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader, -loaderpluginname, org.eclipse.jdt.junit4.runtime, -classNames, easyway.tbs.app.swtbot.st.SellSingleTicketEditorTest, -application, org.eclipse.swtbot.eclipse.core.swtbottestapplication, -product, org.eclipse.platform.ide, -testpluginname, easyway.tbs.app.swtbot]
-version, 3, 描述版本
-port, 60193, 描述端口
-testLoaderClass, org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader, 描述类加载器
-loaderpluginname, org.eclipse.jdt.junit4.runtime, 描述加载插件名称
-classNames, easyway.tbs.app.swtbot.st.SellSingleTicketEditorTest, 描述junit测试类
-application, org.eclipse.swtbot.eclipse.core.swtbottestapplication, 描述测试应用的类型为swtbot test
-product, org.eclipse.platform.ide, 描述具体swtbot测试的product对象
-testpluginname, easyway.tbs.app.swtbot] 描述当前 swtbot 的 junit所在的插件的名称
3.紧接着调用RemotePluginTestRunner的main方法通过类加载器调用特定类的特定方法.
/** * The main entry point. Supported arguments in addition * to the ones supported by RemoteTestRunner: * <pre> * -testpluginname: the name of the plugin containing the tests. * </pre> * @see RemoteTestRunner */ public static void main(String[] args) { RemotePluginTestRunner testRunner = new RemotePluginTestRunner(); testRunner.init(args); testRunner.run(); } /** * Returns the Plugin class loader of the plugin containing the test. * @see RemoteTestRunner#getTestClassLoader() */ protected ClassLoader getTestClassLoader() { final String pluginId = fTestPluginName; return getClassLoader(pluginId); } public ClassLoader getClassLoader(final String pluginId) { Bundle bundle = Platform.getBundle(pluginId); if (bundle == null) throw new IllegalArgumentException("No Classloader found for plug-in " + pluginId); //$NON-NLS-1$ return new BundleClassLoader(bundle); } public void init(String[] args) { readPluginArgs(args); defaultInit(args); } public void readPluginArgs(String[] args) { for (int i = 0; i < args.length; i++) { if (isFlag(args, i, "-testpluginname")) //$NON-NLS-1$ fTestPluginName = args[i + 1]; if (isFlag(args, i, "-loaderpluginname")) //$NON-NLS-1$ fLoaderClassLoader = getClassLoader(args[i + 1]); } if (fTestPluginName == null) throw new IllegalArgumentException("Parameter -testpluginnname not specified."); //$NON-NLS-1$ if (fLoaderClassLoader == null) fLoaderClassLoader = getClass().getClassLoader(); } protected Class loadTestLoaderClass(String className) throws ClassNotFoundException { return fLoaderClassLoader.loadClass(className); }
综上所述:
只需要调整RemotePluginTestRunner.main(Platform.getCommandLineArgs());的main方法的参数即可.但是由于RemoteTestRunner为eclipse 限制访问类org.eclipse.jdt.internal.junit.runner.RemoteTestRunner所以停止研究.