【问题标题】:TestNG dependsOnMethods from different classTestNG 依赖于不同类的方法
【发布时间】:2011-12-03 06:37:46
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java testing junit automated-tests testng


    【解决方案1】:

    两种解决方案: 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>
    
    1. 使用编程:

      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>
      

    【讨论】:

      【解决方案2】:

      您可以在 TestNG @Test 注释中使用 groupsdependsOnGroups,如之前的答案中所述。

      但是,两个类需要在同一个&lt;test&gt;下。

      <!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>
      

      【讨论】:

        【解决方案3】:

        DependsOnMethods 不能从不同的类中使用,为了解决这个问题,我们可以使用dependsOnGroups;

        更改代码;

        1。依赖组类;

        @Test(groups={"先决条件" })

        public void M1()
        {
        
        }
        

        2。调用dependOnGroups的类;

        @Test(dependsOnGroups={"prerequisites"})
        public void M2()
        
        {
        
        }
        

        3。 xml

        <groups>
            <run>
                <include name ="prerequisites"/>
            </run>
        </groups>
        

        【讨论】:

          【解决方案4】:

          将方法放在group 中并使用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
            }
          }
          

          建议在@Before* 中验证配置,如果出现问题则抛出,这样测试就不会运行。这样,测试就可以专注于测试。

          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
            }
          }
          

          【讨论】:

          • 这在运行类文件时工作正常,同时通过 testing.xml 文件运行它得到错误 DependencyMap::Method "LoanApprovalTest.testLoanApprova(java.util.Hashtable)[pri:0, instance :com.zions.release1.Sanity.LoanTestCases.LoanApprovalTest@3884b2]" 依赖于不存在的组 "CreateLoanAccountTest.testCreateLoanAccount"
          • @ArpanSaini 两个类都需要在同一个测试下。
          • 组名(c1)中需要写类名吗?如果我想将不同类别的测试方法归为一组怎么办?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-16
          • 1970-01-01
          • 2014-03-05
          • 1970-01-01
          • 2014-07-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多