需要在RCP应用程序中运行TestNG选项

时间:2017-03-15 11:05:55

标签: java eclipse eclipse-plugin swt eclipse-rcp

我已经实现了一个插件项目,并希望在此应用程序中使用TestNG选项卡作为运行器目的。我有JUnit的解决方案,但在TestNG我仍然被卡住了。请帮助摆脱这种情况。请在下面找到JUnit配置标签代码:

public void createTabs(ILaunchConfigurationDialog dialog, String mode) {
    ILaunchConfigurationTab[] tabs = new ILaunchConfigurationTab[] { 
        new JUnitLaunchConfigurationTab(), 
        new JavaArgumentsTab(), 
        new JavaClasspathTab(), 
        new JavaJRETab(), 
        new SourceLookupTab(), 
        new EnvironmentTab(), 
        new CommonTab() 
    }; 
    setTabs(tabs); 
} 

请建议。

1 个答案:

答案 0 :(得分:0)

****首先通过扩展AbstractLaunchConfigurationTab ******创建TestTab.java类

private ILaunchConfigurationDialog fLaunchConfigurationDialog;

private final TestNGMainTab testngLaunchTab;

private Button runInUIThread;

/**
 * Constructor to create a new junit test tab
 */
public TestTab() {

    this.testngLaunchTab = new TestNGMainTab();
}

public void createControl(Composite parent) {
    testngLaunchTab.createControl(parent);

    Composite composite = (Composite) getControl();
createSpacer(composite);
    createRunInUIThreadGroup(composite);
}

private void createRunInUIThreadGroup(Composite comp) {
    runInUIThread = new Button(comp, SWT.CHECK);
    runInUIThread.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            updateLaunchConfigurationDialog();
        }
    });
    runInUIThread.setText(PDEUIMessages.PDEJUnitLaunchConfigurationTab_Run_Tests_In_UI_Thread);
    GridDataFactory.fillDefaults().span(2, 0).grab(true, false).applyTo(runInUIThread);
}

private void createSpacer(Composite comp) {
    Label label = new Label(comp, SWT.NONE);
    GridDataFactory.fillDefaults().span(3, 0).applyTo(label);
}

public void initializeFrom(ILaunchConfiguration config) {
    testngLaunchTab.initializeFrom(config);
    updateRunInUIThreadGroup(config);
}

private void updateRunInUIThreadGroup(ILaunchConfiguration config) {
    boolean shouldRunInUIThread = true;
    try {
        shouldRunInUIThread = config.getAttribute(IPDELauncherConstants.RUN_IN_UI_THREAD, true);
    } catch (CoreException ce) {
    }
    runInUIThread.setSelection(shouldRunInUIThread);
}

public void performApply(ILaunchConfigurationWorkingCopy config) {
    testngLaunchTab.performApply(config);
    boolean selection = runInUIThread.getSelection();
    config.setAttribute(IPDELauncherConstants.RUN_IN_UI_THREAD, selection);
}

@Override
public String getId() {
    return IPDELauncherConstants.TAB_TEST_ID;
}

@Override
public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
    testngLaunchTab.activated(workingCopy);
}

@Override
public boolean canSave() {
    return testngLaunchTab.canSave();
}

@Override
public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
    testngLaunchTab.deactivated(workingCopy);
}

@Override
public void dispose() {
    testngLaunchTab.dispose();
}

@Override
public String getErrorMessage() {
    return testngLaunchTab.getErrorMessage();
}

@Override
public Image getImage() {
    return testngLaunchTab.getImage();
}

@Override
public String getMessage() {
    return testngLaunchTab.getMessage();
}

public String getName() {
    return testngLaunchTab.getName();
}

@Override
public boolean isValid(ILaunchConfiguration config) {
    return testngLaunchTab.isValid(config);
}

public void setDefaults(ILaunchConfigurationWorkingCopy config) {
    testngLaunchTab.setDefaults(config);
}

@Override
public void setLaunchConfigurationDialog(ILaunchConfigurationDialog dialog) {
    testngLaunchTab.setLaunchConfigurationDialog(dialog);
    this.fLaunchConfigurationDialog = dialog;
}

***创建界面ITestNGPluginLauncherConstants *****

public interface ITestNGPluginLauncherConstants {

String LEGACY_UI_TEST_APPLICATION =

" org.testng.eclipse.runtime.legacytestapplication&#34 ;;

字符串NON_UI_THREAD_APPLICATION =

" org.testng.eclipse.runtime.nonuithreadtestapplication&#34 ;;

字符串UI_TEST_APPLICATION =

" org.testng.eclipse.runtime.uitestapplication&#34 ;;

String CORE_TEST_APPLICATION =

" org.testng.eclipse.runtime.coretestapplication&#34 ;;

String TestNGProgramBlock_headless ="没有申请[无头]";

字符串TAB_PLUGIN_TESTNG_MAIN_ID =

" org.testng.eclipse.plugin.launch.tab.main&#34 ;;

}

****创建类PluginTestNGMainTab.java *****

public class TestNGPluginTabGroup extends
AbstractLaunchConfigurationTabGroup  {

    public TestNGPluginTabGroup() {
    }

    public void createTabs(ILaunchConfigurationDialog dialog, String mode) {

        ILaunchConfigurationTab[] tabs = new ILaunchConfigurationTab[] {

                        new TestTab(),
                        new PluginTestNGMainTab(),
                        new JavaArgumentsTab(), 
                        new PluginsTab(false),
                        new JavaArgumentsTab(),
                        new PluginsTab(), 
                        new TracingTab(),
                        new ConfigurationTab(true), 
                        new TracingTab(), 
                        new EnvironmentTab(), 
                        new CommonTab()
        };

                setTabs(tabs);
                  } 

}

****创建类TestNGProgramBlock.java扩展ProgramBlock *****

public TestNGProgramBlock(AbstractLauncherTab tab) {

    super(tab);
    }

public void setDefaults(ILaunchConfigurationWorkingCopy config) {

if (!LauncherUtils.requiresUI(config))
config.setAttribute(IPDELauncherConstants.APPLICATION, 
ITestNGPluginLauncherConstants.CORE_TEST_APPLICATION);

else
super.setDefaults(config);  
}

protected String[] getApplicationNames() {

TreeSet result = new TreeSet();
         result.add(ITestNGPluginLauncherConstants.TestNGProgramBlock_headless);
String[] appNames = super.getApplicationNames();        
for (int i = 0; i < appNames.length; i++) {
result.add(appNames[i]);
}
return appNames;
}
protected void initializeApplicationSection(ILaunchConfiguration config) 
throws CoreException {
String application = 
config.getAttribute(IPDELauncherConstants.APPLICATION, (String)null);
if(ITestNGPluginLauncherConstants.CORE_TEST_APPLICATION.
equals(application))

fApplicationCombo.setText(ITestNGPluginLauncherConstants.
TestNGProgramBlock_headless); 
else

super.initializeApplicationSection(config); 
}
protected void saveApplicationSection(ILaunchConfigurationWorkingCopy config)
{
if(fApplicationCombo.getText().equals(ITestNGPluginLauncherConstants.
TestNGPogramBlock_headless)){

String appName = fApplicationCombo.isEnabled() ? 

ITestNGPluginLauncherConstants.CORE_TEST_APPLICATION : null;    

config.setAttribute(IPDELauncherConstants.APPLICATION, appName);    

config.setAttribute(IPDELauncherConstants.APP_TO_TEST, (String)null);
}

}   
}