Java TestNG依赖于来自不同类的方法

Java TestNG依赖于来自不同类的方法,java,testing,junit,automated-tests,testng,Java,Testing,Junit,Automated Tests,Testng,当要依赖的测试与具有此注释的测试在同一类中时,@Test注释的dependsOnMethods属性工作正常。但是,如果待测试方法和依赖方法在不同的类中,则它不起作用。示例如下: class c1 { @Test public void verifyConfig() { //verify some test config parameters } } class c2 { @Test(dependsOnMethods={"c1.verifyConfig"}) publ

当要依赖的测试与具有此注释的测试在同一类中时,
@Test
注释的
dependsOnMethods
属性工作正常。但是,如果待测试方法和依赖方法在不同的类中,则它不起作用。示例如下:

class c1 {
  @Test
  public void verifyConfig() {
    //verify some test config parameters
  }
}

class c2 {
  @Test(dependsOnMethods={"c1.verifyConfig"})
  public void dotest() {
    //Actual test
  }
}

有没有办法绕过这个限制?一个简单的解决方法是在
类c2
中创建一个调用
c1.verifyConfig()
的测试。但是这会有太多的重复。

将该方法放在
组中
并使用
dependsOnGroups

class c1 {
  @Test(groups={"c1.verifyConfig"})
  public void verifyConfig() {
    //verify some test config parameters
  }
}

class c2 {
  @Test(dependsOnGroups={"c1.verifyConfig"})
  public void dotest() {
    //Actual test
  }
}
建议在*之前验证
@中的配置,并在出现问题时抛出,以便测试不会运行。这样,测试可以只关注测试

class c2 {
  @BeforeClass
  public static void verifyConfig() {
    //verify some test config parameters
    //Usually just throw exceptions
    //Assert statements will work
  }

  @Test
  public void dotest() {
    //Actual test
  }
}
DependsOnMethods不能从不同的类中使用,为了解决这个问题,我们可以使用dependsonGroup; 做代码变更; 1.dependsOnGroups类; @测试(组={“先决条件”})

2.调用dependsOnGroups的类; 3.xml
您可以在TestNG
@Test
注释中使用
依赖组

但是,这两个类需要位于相同的


运行测试套件时,以下情况将导致异常

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="1" >
  <test name="Test1" >
    <classes>
       <class name="c1" />
    </classes>
  </test>

  <test name="Test2">
    <classes>
      <class name="c2" />
    </classes>
  </test>
</suite>

两种解决方案: 1.使用dependsOnGroups和继承

    import static org.testng.Assert.fail;
    import org.testng.annotations.Test;

    public class PTest1 {
        @Test(groups = "A")
        public void test11() {
            System.out.println("test11");
            fail();
        }
    }


    import org.testng.annotations.Test;
    public class PTest2 extends PTest1 {

        @Test(groups = "B", dependsOnGroups = "A")
        public void test21() {
            System.out.println("test21");
        }
    }


    <suite name="priority" verbose="1">
        <groups>
            <run>
                <include name ="B"/>
            </run>
        </groups>
        <test name="pri2">
            <classes>            
                <class name="priority.PTest2"/>
            </classes>
        </test>
        <test name="pri1">
            <classes>            
                <class name="priority.PTest1"/>
            </classes>
        </test>    
    </suite>
导入静态org.testng.Assert.fail;
导入org.testng.annotations.Test;
公共类PTest1{
@测试(组=“A”)
公开作废测试11(){
System.out.println(“test11”);
失败();
}
}
导入org.testng.annotations.Test;
公共类PTest2扩展了PTest1{
@测试(groups=“B”,dependsOnGroups=“A”)
公共空间测试21(){
System.out.println(“test21”);
}
}
  • 使用编程:

    import static org.testng.Assert.fail;
    import org.testng.annotations.Test;
    public class PTest3 {
    
        @Test
        public void test31() {
            System.out.println("test31");
            fail();
        }
    }
    
    import org.testng.IInvokedMethod;
    import org.testng.ITestContext;
    import org.testng.SkipException;
    import org.testng.annotations.Test;
    
    public class PTest4 {
    
        @Test
        public void test41(ITestContext context) {
            for (IInvokedMethod iInvokedMethod : context.getSuite().getAllInvokedMethods()) {
                if (iInvokedMethod.getTestMethod().getMethodName().equals("test31")
                        && !iInvokedMethod.getTestResult().isSuccess()) {
                    throw new SkipException("test31 is not sucessful!");
                }
            }
            System.out.println("test41");
        }
    }
    
    <suite name="priority" verbose="1">
        <test name="pri3">
            <classes>            
                <class name="priority.PTest3"/>
            </classes>
        </test>
        <test name="pri4">
            <classes>            
                <class name="priority.PTest4"/>
            </classes>
        </test>    
    </suite>
    
    导入静态org.testng.Assert.fail;
    导入org.testng.annotations.Test;
    公共类PTest3{
    @试验
    公开测试31(){
    System.out.println(“test31”);
    失败();
    }
    }
    导入org.testng.IInvokedMethod;
    导入org.testng.ITestContext;
    导入org.testng.SkipException;
    导入org.testng.annotations.Test;
    公共类PTest4{
    @试验
    公共void test41(ITestContext上下文){
    对于(IInvokedMethod IInvokedMethod:context.getSuite().getAllInvokedMethods()){
    如果(iInvokedMethod.getTestMethod().getMethodName().equals)(“test31”)
    &&!iInvokedMethod.getTestResult().isSuccess()){
    抛出新的SkipException(“test31不成功!”);
    }
    }
    System.out.println(“test41”);
    }
    }
    

  • 当运行类文件individual,同时通过testing.xml文件getting error DependencyMap::Method“LoanApprovalTest.testLoanApprova(java.util.Hashtable)[pri:0,instance:com.zions.release1.Sanity.LoanTestCases]运行它时,这可以正常工作。LoanApprovalTest@3884b2]“依赖于不存在的群体“CreateLoanaCountTest.TestCreateLoanaCount”@ArpanSaini这两个类需要进行相同的测试。是否需要在组名(c1)中写入类名?如果我想将来自不同类的测试方法合并到一个组中该怎么办?
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="Suite" verbose="1" >
      <test name="Test" >
        <classes>
           <class name="c1" />
           <class name="c2" />
        </classes>
      </test>
    </suite>
    
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="Suite1" verbose="1" >
      <test name="Test1" >
        <classes>
           <class name="c1" />
        </classes>
      </test>
    
      <test name="Test2">
        <classes>
          <class name="c2" />
        </classes>
      </test>
    </suite>
    
        import static org.testng.Assert.fail;
        import org.testng.annotations.Test;
    
        public class PTest1 {
            @Test(groups = "A")
            public void test11() {
                System.out.println("test11");
                fail();
            }
        }
    
    
        import org.testng.annotations.Test;
        public class PTest2 extends PTest1 {
    
            @Test(groups = "B", dependsOnGroups = "A")
            public void test21() {
                System.out.println("test21");
            }
        }
    
    
        <suite name="priority" verbose="1">
            <groups>
                <run>
                    <include name ="B"/>
                </run>
            </groups>
            <test name="pri2">
                <classes>            
                    <class name="priority.PTest2"/>
                </classes>
            </test>
            <test name="pri1">
                <classes>            
                    <class name="priority.PTest1"/>
                </classes>
            </test>    
        </suite>
    
    import static org.testng.Assert.fail;
    import org.testng.annotations.Test;
    public class PTest3 {
    
        @Test
        public void test31() {
            System.out.println("test31");
            fail();
        }
    }
    
    import org.testng.IInvokedMethod;
    import org.testng.ITestContext;
    import org.testng.SkipException;
    import org.testng.annotations.Test;
    
    public class PTest4 {
    
        @Test
        public void test41(ITestContext context) {
            for (IInvokedMethod iInvokedMethod : context.getSuite().getAllInvokedMethods()) {
                if (iInvokedMethod.getTestMethod().getMethodName().equals("test31")
                        && !iInvokedMethod.getTestResult().isSuccess()) {
                    throw new SkipException("test31 is not sucessful!");
                }
            }
            System.out.println("test41");
        }
    }
    
    <suite name="priority" verbose="1">
        <test name="pri3">
            <classes>            
                <class name="priority.PTest3"/>
            </classes>
        </test>
        <test name="pri4">
            <classes>            
                <class name="priority.PTest4"/>
            </classes>
        </test>    
    </suite>