需要获取TestNG来运行我的Spock测试,因为系统的其余部分都使用TestNG。
由于TestNG支持运行JUnit测试,我尝试了以下操作:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="suiteTestName" verbose="1" junit="true">
<test name="myTestName">
<classes>
<class name="mypackage.artifact.SomeArtifactSpecification" />
</classes>
</test>
</suite>
所以这实际上完美地运行了Groovy'SomeArtiFactSpecification'Spock单元测试。我可以从测试中看到一些I/O,但由于我是从Eclipse中运行的,所以我使用调试器来验证所有测试是否正确执行。我的类路径上有testng-6.8.7和junit-4.10。问题是TestNG似乎并不知道它确实做到了!下面是结束输出:
===============================================
myTestName
Total tests run: 0, Failures: 0, Skips: 0
===============================================
有人知道是否可以让TestNG识别这些测试吗?
附加详细信息-为赏金添加
关于血淋淋的细节,我在下面贴了两个堆栈痕迹。第一个是运行TestNG(这不起作用),第二个是直接运行Spock而不运行TestNG。
来自TestNG的第一个堆栈跟踪:
org.testng.remote.RemoteTestNG at localhost:54288
Thread [main] (Suspended (breakpoint at line 65 in ArtifactManagerSpecification))
ArtifactManagerSpecification.$spock_feature_0_0() line: 65
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 601
ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138
ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330
ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311
ParameterizedSpecRunner(BaseSpecRunner).invokeFeatureMethod() line: 285
ParameterizedSpecRunner(BaseSpecRunner).doRunIteration() line: 256
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 601
ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138
ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330
ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311
ParameterizedSpecRunner(BaseSpecRunner).runIteration(Object[], int) line: 223
ParameterizedSpecRunner(BaseSpecRunner).initializeAndRunIteration(Object[], int) line: 214
ParameterizedSpecRunner(BaseSpecRunner).runSimpleFeature() line: 205
ParameterizedSpecRunner(BaseSpecRunner).doRunFeature() line: 199
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 601
ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138
ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330
ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311
ParameterizedSpecRunner(BaseSpecRunner).runFeature() line: 175
ParameterizedSpecRunner(BaseSpecRunner).runFeatures() line: 152
ParameterizedSpecRunner(BaseSpecRunner).doRunSpec() line: 112
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 601
ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138
ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330
ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311
ParameterizedSpecRunner(BaseSpecRunner).runSpec() line: 91
ParameterizedSpecRunner(BaseSpecRunner).run() line: 82
Sputnik.run(RunNotifier) line: 63
JUnitCore.run(Runner) line: 157
JUnitCore.run(Request) line: 136
JUnit4TestRunner.start(Class, String...) line: 81
JUnit4TestRunner.run(Class, String...) line: 69
TestRunner$1.run() line: 682
TestRunner.runWorkers(List<IWorker<ITestNGMethod>>, String, ListMultiMap<Integer,TestMethodWorker>) line: 1005
TestRunner.privateRunJUnit(XmlTest) line: 713
TestRunner.run() line: 614
SuiteRunner.runTest(TestRunner) line: 334
SuiteRunner.runSequentially() line: 329
SuiteRunner.privateRun() line: 291
SuiteRunner.run() line: 240
SuiteRunnerWorker.runSuite(SuiteRunnerMap, XmlSuite) line: 52
SuiteRunnerWorker.run() line: 86
RemoteTestNG(TestNG).runSuitesSequentially(XmlSuite, SuiteRunnerMap, int, String) line: 1224
RemoteTestNG(TestNG).runSuitesLocally() line: 1149
RemoteTestNG(TestNG).run() line: 1057
RemoteTestNG.run() line: 111
RemoteTestNG.initAndRun(String[], CommandLineArgs, RemoteArgs) line: 204
RemoteTestNG.main(String[]) line: 175
直接运行Spock的第二个堆栈跟踪(工作)
Thread [main] (Suspended (breakpoint at line 65 in ArtifactManagerSpecification))
ArtifactManagerSpecification.$spock_feature_0_0() line: 65
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 601
ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138
ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330
ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311
ParameterizedSpecRunner(BaseSpecRunner).invokeFeatureMethod() line: 285
ParameterizedSpecRunner(BaseSpecRunner).doRunIteration() line: 256
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 601
ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138
ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330
ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311
ParameterizedSpecRunner(BaseSpecRunner).runIteration(Object[], int) line: 223
ParameterizedSpecRunner(BaseSpecRunner).initializeAndRunIteration(Object[], int) line: 214
ParameterizedSpecRunner(BaseSpecRunner).runSimpleFeature() line: 205
ParameterizedSpecRunner(BaseSpecRunner).doRunFeature() line: 199
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 601
ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138
ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330
ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311
ParameterizedSpecRunner(BaseSpecRunner).runFeature() line: 175
ParameterizedSpecRunner(BaseSpecRunner).runFeatures() line: 152
ParameterizedSpecRunner(BaseSpecRunner).doRunSpec() line: 112
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 601
ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138
ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330
ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311
ParameterizedSpecRunner(BaseSpecRunner).runSpec() line: 91
ParameterizedSpecRunner(BaseSpecRunner).run() line: 82
Sputnik.run(RunNotifier) line: 63
JUnit4TestClassReference(JUnit4TestReference).run(TestExecution) line: 50
TestExecution.run(ITestReference[]) line: 38
RemoteTestRunner.runTests(String[], String, TestExecution) line: 467
RemoteTestRunner.runTests(TestExecution) line: 683
RemoteTestRunner.run() line: 390
RemoteTestRunner.main(String[]) line: 197
我对此做了进一步的调查,至少找到了原因。
在下面的org.testng.junit.junit4TestMethod中,无法正确解析Spock测试名称。我在猜测,但我认为Spock使用了一种类型的名称mangling来支持它允许的nice text方法名称。因此,即使下面的TestNG代码获得了一个类似“这是我的测试”的方法名,它也不能通过所示的方法解决这个问题。我认为真正的方法名类似于:$SPOCK_FEATURE_0_0
private static Method getMethod(Description desc) {
Class<?> c = desc.getTestClass();
String method = desc.getMethodName();
try {
return c.getMethod(method);
} catch (Throwable t) {
Utils.log("JUnit4TestMethod", 2,
"Method '" + method + "' not found in class '" + c.getName() + "': " + t.getMessage());
return null;
}
}
结果,这会在c.getMethod(method)调用上引发一个异常--它无法将Spock方法名解析为类上的实际方法。
看起来真正“混乱局面”的是,上面的代码是由Junit(org.Junit.runner.notification.runnotifier)在以下代码中调用的:
private abstract class SafeNotifier {
void run() {
synchronized (fListeners) {
for (Iterator<RunListener> all= fListeners.iterator(); all.hasNext();)
try {
notifyListener(all.next());
} catch (Exception e) {
all.remove(); // Remove the offending listener first to avoid an infinite loop
fireTestFailure(new Failure(Description.TEST_MECHANISM, e));
}
}
}
如果这段代码不悄悄地移除监听器,而是将其记录下来,那就真的很好了...
问题: TestNG正在运行所有172个测试,但是它只报告了81个测试运行,没有报告一些失败(实际上有两个测试失败,但只报告了一个)。 似乎未报告的测试是有什么方法可以让TestNG正确识别这些测试并正确报告它们的结果吗?
经过一些谷歌搜索,我没有找到什么地方,决定玩一下,发现如果我执行所有测试,它会运行良好,即使当我改变类名以匹配文件名时,它也会工作。是否有一种方法可以配置Eclipse/JUnit,使其能够在与文件名不匹配的groovy类上运行测试? HelloWorld.groovy:
问题内容: 我正在使用 Junit 通过 Seleniun WebDriver 运行测试。我试图将测试分成功能区域,以更好地报告错误。我创建了测试以测试页面加载/将文档移动到其他工作流程。如果页面加载测试失败,或者工作流移动失败,我想跳过后续的页面/工作流测试。 如果测试A失败,如何跳过班级中的其余测试或在班级B中运行测试? 注意: 我意识到我要问的是“ UNIT TESTS的 不良实践* 。但是
安装程序:我有一个扩展IRetryAnalyzer的类,并实现了一个简单的重试逻辑,覆盖了以下方法:public boolean retry(ITestResult result){ 提前道谢。