[英]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.