2014-01-30 60 views
2

我目前正在编写集成测试,我必须在我的测试运行之前检查一些条件。DependsOnGroups失败,带循环依赖异常

public class TheClassWhichContainsIT { 

    @Test(dependsOnGroups = { "init" }) 
    public void thisIsTheFirstTest() { 
     System.out.printf("Test"); 
    } 

    @Test(groups = "init") 
    public void checkIfWeAreOnWindows() { 
     throw new SkipException("Not on windows"); 
    } 

} 

上面的例子,如果工作在SkipException抛出比测试将被标记为跳过,而不是一个失败,因为我喜欢它的预期。

因此,基于Cedric Beust's blog我想修改我的集成测试分为以下几个:

@Test(dependsOnGroups = { "init" }) 
public class TheClassWhichContainsIT extends IntegrationTestBase { 

    public void thisIsTheFirstTest() { 
     System.out.printf("Test"); 
    } 
} 

@Test(groups = "init") 
public class IntegrationTestBase { 

    public void checkIfWeAreOnWindows() { 
     throw new SkipException("ignore"); 
    } 

} 

但如果我尝试运行我的测试中,我得到了以下内容:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2. 
16:test (default-test) on project testngtest: Execution default-test of goal org 
.apache.maven.plugins:maven-surefire-plugin:2.16:test failed: There was an error 
in the forked process 
[ERROR] org.testng.TestNGException: 
[ERROR] The following methods have cyclic dependencies: 
[ERROR] TheClassWhichContainsTest.thisIsTheFirstTest()[pri:0, instance:de.akdb.k 
[email protected]] 
[ERROR] 
[ERROR] at org.testng.internal.Graph.topologicalSort(Graph.java:148) 
[ERROR] at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:26 
1) 
[ERROR] at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:317) 
[ERROR] at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper. 
java:59) 
[ERROR] at org.testng.TestRunner.initMethods(TestRunner.java:481) 
[ERROR] at org.testng.TestRunner.init(TestRunner.java:235) 
[ERROR] at org.testng.TestRunner.init(TestRunner.java:205) 
[ERROR] at org.testng.TestRunner.<init>(TestRunner.java:153) 
[ERROR] at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRu 
nner.java:522) 
[ERROR] at org.testng.SuiteRunner.init(SuiteRunner.java:157) 
[ERROR] at org.testng.SuiteRunner.<init>(SuiteRunner.java:111) 
[ERROR] at org.testng.TestNG.createSuiteRunner(TestNG.java:1299) 
[ERROR] at org.testng.TestNG.createSuiteRunners(TestNG.java:1286) 
[ERROR] at org.testng.TestNG.runSuitesLocally(TestNG.java:1140) 
[ERROR] at org.testng.TestNG.run(TestNG.java:1057) 
[ERROR] at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.ja 
va:91) 
[ERROR] at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeSing 
leClass(TestNGDirectoryTestSuite.java:128) 
[ERROR] at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(Tes 
tNGDirectoryTestSuite.java:112) 
[ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider 
.java:113) 
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameCla 
ssLoader(ForkedBooter.java:200) 
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(Fork 
edBooter.java:153) 
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java: 
103) 
[ERROR] -> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit 
ch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please rea 
d the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutio 
nException 
+0

您是否找到任何解决方案? –

回答

0

也许你在testng配置文件中有问题

例如,如果你有像这样的testng配置之一:

<test name="some-tests" preserve-order="true"> 
    <groups> 
     <define name="test-group"> 
      <include name="init"/> 
     </define> 
     <run> 
      <include name="test-group"/> 
     </run> 
    </groups> 

    <classes> 
     <class name="TheClassWhichContainsIT"/> 
     <class name="IntegrationTestBase"/> 
    </classes> 
</test> 

物业preserve-order="true"意味着在标签classes定义的类都会在定义的顺序执行。

在这种情况下,我们可能会收到循环依赖和结果错误。