IAnnotation 转换方法未禁用 testng 中的测试
IAnnotation transform method is not disabling the test in testng
我的意图是,根据前面的方法条件,@Test
方法需要启用或禁用但是在下面的代码中,即使我传递了测试用例名称,它也没有被跳过吗?谁能给我建议解决方案?
我需要 BeforeTestMethod
来检查我实际代码中的一些逻辑,基于此我必须在 class 文件
中启用 @Test
public class ListnerClass implements IAnnotationTransformer {
public static String testName;
public void transform(ITestAnnotation iTest, Class testClass, Constructor testConstructor, Method method) {
if(method.getName().equalsIgnoreCase(testName)) {
iTest.setEnabled(false);
}
}
public class TestNGTest3 {
@BeforeMethod
public void setUp(Method result) {
System.out.println("This is the before Method getting name "+result.getName());
if(result.getName().contains("3"))
{
ListnerClass.testName=result.getName();
}
}
@Test
public void testMethod3() {
System.out.println("This is the Method of Method");
}
@Test
public void testMethod4() {
System.out.println("Hi");
}
}
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name=" Regression Suite">
<listeners>
<listener class-name="com.listners.ListnerClass" />
</listeners>
<test thread-count="1" name="Test">
<classes>
<class name="com.test.TestNGTest3" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
输出:
This is the before Method getting name testMethod3
This is the Method of Method
This is the before Method getting name testMethod4
Hi
终于在下面post的帮助下实现了我的意图。
这是一个工作示例:
public class TestClassTest {
static String name;
static int i=0;
`public static String testmethod() {
List<String> list= new ArrayList<String>();
list.add("Raja");
list.add("Raju");
list.add("Raj");
name=list.get(i);
i++;
return name;
}
public class DependsOnMethodClass extends TestClassTest {
@BeforeMethod()
public void beforecaller(Method m) {
if(!testmethod().equals("Raju")) {
System.out.println("This is the method going to be skipped
"+m.getName());
throw new SkipException("Data provided is not matching");
}
else {
System.out.println("This is the method not skipped"+m.getName());
}
}
@Test
public void getCall() {
System.out.println("This is the getCall method");
}
@Test()
public void getCall1() {
System.out.println("This is the getCall1 method");
}
}
TestNG 更新 xml:(重要更改)
添加参数 configfailurepolicy="continue" 将继续测试,即使我们
正在抛出跳过异常。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" configfailurepolicy="continue">
<test thread-count="5" name="Test">
<classes>
<class name="com.testng.newsuite.DependsOnMethodClass"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Output:
This is the method going to be skipped getCall
This is the method not skipped getCall1
This is the getCall1 method
我的意图是,根据前面的方法条件,@Test
方法需要启用或禁用但是在下面的代码中,即使我传递了测试用例名称,它也没有被跳过吗?谁能给我建议解决方案?
我需要 BeforeTestMethod
来检查我实际代码中的一些逻辑,基于此我必须在 class 文件
@Test
public class ListnerClass implements IAnnotationTransformer {
public static String testName;
public void transform(ITestAnnotation iTest, Class testClass, Constructor testConstructor, Method method) {
if(method.getName().equalsIgnoreCase(testName)) {
iTest.setEnabled(false);
}
}
public class TestNGTest3 {
@BeforeMethod
public void setUp(Method result) {
System.out.println("This is the before Method getting name "+result.getName());
if(result.getName().contains("3"))
{
ListnerClass.testName=result.getName();
}
}
@Test
public void testMethod3() {
System.out.println("This is the Method of Method");
}
@Test
public void testMethod4() {
System.out.println("Hi");
}
}
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name=" Regression Suite">
<listeners>
<listener class-name="com.listners.ListnerClass" />
</listeners>
<test thread-count="1" name="Test">
<classes>
<class name="com.test.TestNGTest3" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
输出:
This is the before Method getting name testMethod3
This is the Method of Method
This is the before Method getting name testMethod4
Hi
终于在下面post的帮助下实现了我的意图。
这是一个工作示例:
public class TestClassTest {
static String name;
static int i=0;
`public static String testmethod() {
List<String> list= new ArrayList<String>();
list.add("Raja");
list.add("Raju");
list.add("Raj");
name=list.get(i);
i++;
return name;
}
public class DependsOnMethodClass extends TestClassTest {
@BeforeMethod()
public void beforecaller(Method m) {
if(!testmethod().equals("Raju")) {
System.out.println("This is the method going to be skipped
"+m.getName());
throw new SkipException("Data provided is not matching");
}
else {
System.out.println("This is the method not skipped"+m.getName());
}
}
@Test
public void getCall() {
System.out.println("This is the getCall method");
}
@Test()
public void getCall1() {
System.out.println("This is the getCall1 method");
}
}
TestNG 更新 xml:(重要更改) 添加参数 configfailurepolicy="continue" 将继续测试,即使我们 正在抛出跳过异常。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" configfailurepolicy="continue">
<test thread-count="5" name="Test">
<classes>
<class name="com.testng.newsuite.DependsOnMethodClass"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Output:
This is the method going to be skipped getCall
This is the method not skipped getCall1
This is the getCall1 method