gpt4 book ai didi

junit - 使用自定义 JUnit 运行器实现从 eclipse 插件启动 JUnit 测试

转载 作者:行者123 更新时间:2023-12-01 08:01:19 32 4
gpt4 key购买 nike

我已经编写了一个自定义 JUnit 运行器,我希望它成为 eclipse 插件的一部分,该插件将使用该运行器启动测试,而无需将 @RunWith 注释应用于该类。我已经设法使用 org.eclipse.debug.ui.launchShortcuts 扩展点在“运行方式”上下文菜单下获得了一个附加项。但是,我不确定如何使用我的自定义运行器调用测试。

最佳答案

所以我想出了一个方法来做我想做的事。但是,它似乎有点老套。但是,我想我会在这里发布答案,以防其他人遇到同样的问题。

首先你必须像这样注册一个 junit 类型:

<extension point="org.eclipse.jdt.junit.internal_testKinds">
<kind
id="my.junit.kind"
displayName="Your Kind Name"
finderClass="org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder"
loaderPluginId="org.eclipse.jdt.junit4.runtime"
loaderClass="your.test.loader.MyLoaderClass">
<runtimeClasspathEntry pluginId="org.eclipse.jdt.junit4.runtime" />
<runtimeClasspathEntry pluginId="org.eclipse.jdt.junit.core" />
<runtimeClasspathEntry pluginId="org.eclipse.jdt.junit.runtime"/>
</kind>
</extension>

在 xml 中,您必须指定 org.eclipse.jdt.internal.junit.runner.ITestLoader 的自定义实现,它又返回 org.eclipse.jdt 的实现。 internal.junit.runner.ITestReference。核心部分是 ITestReference 的实现,因为这是您创建自定义 JUnit 运行器实例的地方。

  public class MyTestReference extends JUnit4TestReference
{

public MyTestReference(final Class<?> p_clazz, String[] p_failureNames)
{
super(new Request()
{
@Override
public Runner getRunner()
{
return new MyCustomRunner(p_clazz);
}

}, p_failureNames);
}
...
}

最后,您必须将其与适当设置类型的启动快捷方式相关联

public class MyJunitLaunchShortcut extends JUnitLaunchShortcut
{
@Override
protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement p_element) throws CoreException
{
ILaunchConfigurationWorkingCopy config = super.createLaunchConfiguration(p_element);
config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, "my.junit.kind");
return config;
}
}

这确实使用了一堆内部类,所以可能有更好的方法。但这似乎有效。

关于junit - 使用自定义 JUnit 运行器实现从 eclipse 插件启动 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13282903/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com