TestNG dependsOnMethods from different class(TestNG 依赖于不同类的方法)
问题描述
@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
}
}
有没有办法绕过这个限制?一种简单的方法是在 class 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.
推荐答案
把方法放在一个group
中,使用dependsOnGroups
.
Put the method in a group
and use 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 依赖于不同类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TestNG 依赖于不同类的方法


基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01