3 回答

TA貢獻1921條經驗 獲得超9個贊
我希望跳過當前測試而不是所有剩余測試的問題仍然存在。
結果發現問題出在 Testng 中的 configfailurepolicy 上。當我希望將其設置為繼續(繼續套件的其余部分)時,它默認為跳過(跳過所有剩余的測試)。
這是我在其他地方找到的答案,我設法以兩種不同的方式應用它。鏈接在這里
1. 首先,創建一個 testng.xml 并從那里運行測試。在套件名稱旁邊,添加標簽 configfailurepolicy="continue" 這是我的 testng.xml 下面
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" configfailurepolicy="continue">
<test name="MyTests" preserve-order="true">
<classes>
<class name="testclassLocation..." />
</classes>
</test>
</suite>
如果您這樣做,請確保從 testng.xml 運行測試。
2. 找到 testng 的 .jar 所在位置。我使用的是maven,所以它是“${user.dir}.m2\repository\org\testng\testng\6.14.3”。
然后打開 .jar 存檔,查看文件“testng-1.0.dtd”,找到該行
configfailurepolicy (skip | continue) "skip"
并將其更改為
configfailurepolicy (skip | continue) "continue"
之后應該可以正常工作。
編輯:正如評論中提到的,建議使用第一個解決方案,因為它允許這些更改/修復可以跨多個項目/設備移植。第二種解決方案只會將修復應用于您的計算機。

TA貢獻1820條經驗 獲得超10個贊
SkipException就是您正在尋找的。
在 中beforeMethod
,檢查您的數據,SkipException
如果不正確則拋出。這將跳過測試。在這個完整而簡單的示例中,test2
跳過:
import org.testng.SkipException;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
public class TestA {
? ? @Retention(RetentionPolicy.RUNTIME)
? ? public @interface MyCustomAnnotation {
? ? ? ? boolean dataCorrect();
? ? }
? ? @BeforeMethod
? ? public void beforeMethod(Method m) {
? ? ? ? if (!m.getAnnotation(MyCustomAnnotation.class).dataCorrect()) {
? ? ? ? ? ? throw new SkipException("Invalid data");
? ? ? ? }
? ? }
? ? @Test
? ? @MyCustomAnnotation(dataCorrect = true)
? ? public void test1() {
? ? ? ? System.out.println("test1");
? ? }
? ? @Test
? ? @MyCustomAnnotation(dataCorrect = false)
? ? public void test2() {
? ? ? ? System.out.println("test2");
? ? }
}
您還需要更改默認配置失敗策略,以便即使跳過一個測試也可以運行其他測試。這是在套件級別完成的:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" configfailurepolicy="continue">
...
</suite>
感謝@Kyle OP指出這個屬性。

TA貢獻1893條經驗 獲得超10個贊
在BeforeMethod中創建一個靜態Booleanflag,如果數據不正確,則只需將標志更改為true,然后在布爾為true的測試中首先將標志更改為false并失敗測試
示例代碼
public static Boolean myflag=false;
@BeforeMethod
public void beforeMethod(Method m){
//reads code
if (dataNotCorrect)
myflag=true;
}
@Test @MyCustomAnnotation(data = incorrect)
public void Test1(){
if(myflag){
myflag=false;
s_assert.fail();
}
添加回答
舉報