問題描述
@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
中創(chuàng)建一個調(diào)用 c1.verifyConfig()
的測試.但這將是太多的重復(fù).
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
* 中驗證配置并在出現(xiàn)問題時拋出,這樣測試就不會運(yùn)行.這樣,測試就可以專注于測試.
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
}
}
這篇關(guān)于TestNG 依賴于不同類的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!