org.testng.TestNGException: 以下方法在通过TestNG执行测试时存在循环依赖

org.testng.TestNGException: The following methods have cyclic dependencies while executing tests through TestNG

Testng异常显示循环依赖请解释

package test.depends;
import org.testng.annotations.Test
public class SimpleDependencyTes {
@Test
public void testOne() {
    System.out.println("The first method");
}
@Test(dependsOnMethods= {"testOne","testTwo"})
public void testTwo() {
    System.out.println("The Second method");
}
}

错误如下:

org.testng.TestNGException: The following methods have cyclic dependencies:SimpleDependencyTes.testTwo()[pri:0, instance:test.depends.SimpleDependencyTes@1774679]
at org.testng.internal.Graph.topologicalSort(Graph.java:149)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:261)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:317)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:64)
at org.testng.TestRunner.initMethods(TestRunner.java:438)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

您的 testTwo 将自己列为依赖项:

@Test(dependsOnMethods= {"testOne","testTwo"})
public void testTwo() {

根据定义dependsOnMethods表示

(testTwo) will start execution only after all the tests it depends on executed successfully

(source)。因此,如果您按上面引用的方式设置 testTwo,则表示在 testTwo 成功执行之前,testTwo 无法启动。这是不可能的。相反,您可能希望 testTwo 仅依赖于 testOne

@Test(dependsOnMethods= {"testOne"})
public void testTwo() {

还有一些其他的testThree可以同时依赖:

@Test(dependsOnMethods= {"testOne","testTwo"})
public void testThree() {

你定义了两个方法的依赖: testOne 和 testTwo ,其中 testTwo 是它自己,这并不意味着。您不能在方法本身上定义依赖关系。您可以定义对任何其他方法的依赖性。所以你需要删除 testTwo 依赖。

@Test(dependsOnMethods= {"testOne"})
public void testTwo() {
    System.out.println("The Second method");
}

您可以为任何第三个@Test 方法定义这两个依赖项,例如:

@Test(dependsOnMethods= {"testOne","testTwo"})
public void testThree() {
    System.out.println("The third method");
}

循环依赖

Circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.

循环依赖可能发生在两种情况下:

  • 方法 testOne() 引用了另一个方法 testTwo(),同时 testTwo() 正在尝试引用 testOne().
  • 方法 testTwo() 正在尝试引用自身,即 testTwo()

在这里你可以找到关于

的详细讨论

dependsOnMethods

dependsOnMethods用于创建一个List的方法,这个方法依赖于


带有@Test注解的dependsOnMethods

有两种依赖:

  • Hard dependencies: 你所依赖的所有方法都必须运行并且为你成功运行。如果您的依赖项中至少发生一次失败,则该方法将不会被调用并在报告中标记为 SKIP。
  • 软依赖:方法总是在你依赖的方法之后运行,即使其中一些失败了。当您只想确保您的测试方法按特定顺序 运行 但它们的成功并不真正取决于其他人的成功时,这很有用。通过在 @Test 注释中添加 "alwaysRun=true" 获得软依赖。

  • 一个例子:

    @Test
    public void initial_test_method() {}
    
    @Test(dependsOnMethods = { "serverStartedOk" })
    public void test_method1() {}
    

    在此示例中,test_method1() 声明为依赖于方法 initial_test_method(),这保证了 initial_test_method() 将始终首先被调用。


出了什么问题

在您的程序中,testTwo()@Test 注释包含 testTwo()(本身)作为 dependsOnMethods 之一。因此您会看到错误。


解决方案

如果您从 dependsOnMethodsList 中删除方法 testTwo(),您的程序将是完美的。

package test.depends;
import org.testng.annotations.Test
public class SimpleDependencyTes {
    @Test
    public void testOne() {
        System.out.println("The first method");
    }
    @Test(dependsOnMethods= {"testOne"})
    public void testTwo() {
        System.out.println("The Second method");
    }
}