从不同的类TestNG的dependsOnMethods [英] TestNG dependsOnMethods from different class

查看:3892
本文介绍了从不同的类TestNG的dependsOnMethods的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Test 标注的 dependsOnMethods 属性工作正常时,试验可依靠的是在同一类作为有这个注释的测试。但是,如果要被测试的方法和依赖-时方法,可在不同的类不起作用。实施例是如下:

The dependsOnMethods attribute of the @Test annotation works fine when the test to be depended upon is in the same class as that of the test that has this annotation. But it does not work if the to-be-tested method and depended-upon method are in different classes. Example is as follows:

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()。但是,这将太多的重复。

Is there any way to get around this limitation? One easy way out is to create a test in class c2 that calls c1.verifyConfig(). But this would be too much repetition.

推荐答案

把方法在,并使用 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 *配置,如果出现错误有那么测试将不会运行抛出。这样的测试可以专注于只是测试。

It is recommended to verify configuration in a @Before* and throw if something goes wrong there so the tests won't run. This way the tests can focus on just testing.

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

这篇关于从不同的类TestNG的dependsOnMethods的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆