Change default “Program to Run” in Eclipse launch configuration

◇◆丶佛笑我妖孽 提交于 2019-12-08 16:03:41

问题


every now and then I am launching JUnit tests from within Eclipse by using

Run As > JUnit Plug-in Test

By default Eclipse assumes you are running something which requires a workbench and chooses in the "Main" section of the launch configuration

LaunchConfig > Main > Program To Run > Run an application > org.eclipse.ui.ide.workbench

I can understand why this is the default, but for me (and for all in our team) this is never ever the case. We always need to run our JUnit Plug-in Tests as

LaunchConfig > Main > Program To Run > Run an application > [No Application] Headless Mode

How do I change this default behaviour? I am currently using Eclipse 4.4.


回答1:


It seems a custom LaunchConfiguration-Extension is a viable solution attempt. What I did was to create a new, custom LaunchConfiguration-Extension which is 99.999% build on the JUnitLaunchConfiguration. I only had to add a custom

BlaBlaJUnitPluginTestLauncher extends launching.JUnitLaunchConfigurationDelegate

which overrides the

launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)

method to adjust the application parameters according to our needs.

BlaBlaJUnitPluginTestTabGroup extends org.eclipse.pde.ui.launcher.JUnitTabGroup

To be able to initialize the LaunchConfig dialog with the default parameter, I had to:

  1. Add a custom BlaBlaPluginJUnitMainTab extends PluginJUnitMainTab
  2. Create a custom JUnitProgramBlock implementation (BlaBlaJUnitProgramBlock)
  3. Creating an instance of BlaBlaJUnitProgramBlock in the BlaBlaJUnitPluginTestTabGroup.BlaBlaPluginJUnitMainTab.createProgramBlock() method
  4. Overriding setDefaults-method (not sure if its really neccessary) in BlaBlaJUnitProgramBlock
  5. Overriding initializeForm-method in the BlaBlaJUnitProgramBlock and adjust parameter there too.

Leading to the following result:




回答2:


This answer is a near miss:

Try this:

  1. Manually create and configure one "good" launch configuration.

Next time you want to launch a test that doesn't yet have a good launch configuration:

  1. Select the file and invoke Run As > Run Configurations ... (i.e., don't yet select JUnit Plug-in Test!)
  2. In that dialog select a good launch configuration of the same kind, and ...
  3. Then click New Launch Configuration (upper-left corner)

Now the newly created configuration should "inherit" the configured values from the the good configuration.

Truth is:

  • You can duplicate an existing launch configuration (leaving you to manually select the test to launch)
  • The Debug team once had plans to support launch configuration templates.

Edit 2018:

Since Eclipse Photon, the Java debugger supports launch configuration prototypes. I just filed Bug 536728 to request this also for test launches. Feel free to chime in (or contribute) on that bug.




回答3:


if u r looking for only shortcut for convenience then eclipse remembers last execution. After using run as and saving ur run config, just use "Run as" button in toolbar. besides this eclipse comes with flavour for testers, u can check that out. Also since you are talking about unit testing see if you can make use of ant build or even better converting to maven based project. Maven has integrated support for testing.




回答4:


There seems to be a simple and effective heuristic in place, which decides whether or not a JUnit Plug-in Test should be run headlessly or with an application:

Make sure that the plug-in containing your tests has no dependencies on anything org.eclipse.ui.

  • Without that dependency [No Application - Headless Mode] is selected by default for newly created launch configurations.
  • With that dependency the default is Run a product, with s.t. like org.eclipse.platform.ide preselected.


来源:https://stackoverflow.com/questions/30642812/change-default-program-to-run-in-eclipse-launch-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!