[英]How to Skip only one method in Selenium
在硒中,作為使用testng的混合框架的一部分,如果在Excel工作表中對該測試用例設置了“否”,則應跳過該用例。
注意 :測試用例名稱和方法名稱也相同。
就是這種情況:我正在使用TestNG的BeforeMethod和Test注釋。 在Excel工作表中,我是這樣的
TCID Description Runmode TestCaseA1 vvvvvvvvvvvvvvvv N TestCaseA2 vvvvvvvvvvvvvvvv Y
由於第一種情況設置為“否”,因此即使將下一種情況設置為“是”也將被跳過。
以下是代碼。 請提供解決問題的方法。
@BeforeMethod
public void checkTestSkip(Method method) throws IOException{
intialise();//to load the properties
String testName = method.getName();
if (!testUtil.isTestCaseRunnable(SuiteAXls,testName)) {
App_Logs.info("Skipping the case as set to No");
throw new SkipException("Skipping the case as set to No");
}
else {
App_Logs.info("Not Skipping");
}
}
@Test(priority=1)
public void TestCaseA1(){
System.out.println("test case A1");
}
@Test(priority=2)
public void TestCaseA2(){
System.out.println("test case A11");
}
}
********************************************
public static boolean isTestCaseRunnable(Xls_Reader xls,String testName){
boolean runmode=false;
int rwcnt=xls.getRowCount("Test Cases");
for(int j=2;j<=rwcnt;j++){
//for(int i=0;i<method.length;i++){
//System.out.println(method[i].getName());
System.out.println(xls.getCellData("Test Cases","TCID",j));
if(testName.equals(xls.getCellData("Test Cases","TCID",j))){
if(xls.getCellData("Test Cases","Runmode",j).equals("Y")){
System.out.println("runmode is yes");
runmode=true;
break;
}else{
System.out.println("runmode is false");
runmode=false;
break;
}
}
}
return runmode;
}
如果要將其作為TestNG函數進行操作,這並非易事-您必須以編程方式運行整個套件,通常需要進行大量重寫。
但是,您可以僅使用布爾值來執行此操作,而將TestNG排除在外。
在測試是否將其設置為“ no”的if
子句中,您只需將靜態變量設置為電子表格單元格的值即可。 例如
@BeforeSuite
public static boolean runTest = true;
...
if (!spreadsheet.says.yes)
runTest = false;
現在,在另一堂課中...
@Test
if (firstClass.runTest)
doTheThing()
else
System.out.println("[INFO] Skipping test...");
使用IMethodInterceptor
可以輕松實現。
假設您只有兩種測試方法:
@Listeners(SkipInterceptor.class)
public class TestMethodCandidates {
@Test
public void checkAddition() {
System.out.println("I am in checkAddition!");
assertEquals(1+2, 3, "1+2 should equal 3.");
}
@Test
public void shouldBeSkipped() {
System.out.println("I am in shouldBeSkipped!");
assertEquals(true, true, "This anti-pattern should be skipped.");
}
}
和方法攔截器:
public class SkipInterceptor implements IMethodInterceptor {
/**
* Your spreadsheet variable.
*/
private Reader reader;
public List<IMethodInstance> intercept(List<IMethodInstance> list, ITestContext iTestContext) {
return list.stream()
.filter(instance ->
!isSkippable(instance.getMethod().getMethodName()))
.collect(Collectors.toList());
}
/**
* In this method you can use your spreadsheet and name of the method.
* @return true, iff this method should run.
*/
private boolean isSkippable(String methodName) {
if(methodName.equals("shouldBeSkipped")) {
return true;
}
return false;
}
}
我的輸出:
I am in checkAddition!
Test ignored.
IMethodInterceptor
攔截您要調用的測試方法,然后檢查是否應跳過它們。 在isSkippable
方法中,您可以使用電子表格。 如果您希望使用更短的解決方案,甚至可以使用intercept
方法進行管理。
讓我知道,如果您喜歡Java 7。
您可以在套件本身中使用排除方法。
<methods>
<exclude name="Test method name to exclude"/>
</methods>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.