IAnnotation 转换方法未在 TestNG 中禁用测试。

huangapple go评论92阅读模式
英文:

IAnnotation transform method is not disabling the test in testng

问题

这里我的意图是,根据前面的方法条件,@Test 方法需要启用或禁用,但在下面的代码中,即使我传递了测试用例名称,它也没有被跳过? 有人能给我提供解决方案吗?

我需要BeforeTestMethod在我的实际代码中检查一些逻辑,并根据这些逻辑来启用类文件中的@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>
</suite>

输出:

This is the before Method getting name testMethod3
This is the Method of Method
This is the before Method getting name testMethod4
Hi
英文:

Here my intention is , Based on the before method condition the @Test method needs to enabled or disabled But in the below code even though I am passing the test case name its not getting skipped ? Can anyone suggest me solution?

I need the BeforeTestMethod to check some logic in my actual code and based on that I have to enable the @Test in the class file

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(&quot;This is the before Method getting name &quot;+result.getName());
	if(result.getName().contains(&quot;3&quot;)) 
       {
		ListnerClass.testName=result.getName();

	}
}

@Test
public void testMethod3() {
	System.out.println(&quot;This is the Method of Method&quot;);
}

@Test
public void testMethod4() {

	System.out.println(&quot;Hi&quot;);
}


	
}

TestNG.xml

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot;&gt;
    &lt;suite name=&quot; Regression Suite&quot;&gt;
    &lt;listeners&gt;
	&lt;listener class-name=&quot;com.listners.ListnerClass&quot; /&gt;
    &lt;/listeners&gt;
    &lt;test thread-count=&quot;1&quot; name=&quot;Test&quot;&gt;
	&lt;classes&gt;
		&lt;class name=&quot;com.test.TestNGTest3&quot; /&gt;
	&lt;/classes&gt;
&lt;/test&gt; &lt;!-- Test --&gt;
&lt;/suite&gt; &lt;!-- Suite --&gt;

Output:

This is the before Method getting name testMethod3
This is the Method of Method
This is the before Method getting name testMethod4
Hi

答案1

得分: 0

终于,在下面的帖子的帮助下,我实现了我的意图。

https://stackoverflow.com/questions/46888283/why-is-throw-new-skipexception-skipping-all-my-methods

这是一个工作示例:

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
英文:

Finally I have achieve my intention with the help of the below post.

https://stackoverflow.com/questions/46888283/why-is-throw-new-skipexception-skipping-all-my-methods

Here is an working sample:

 public class TestClassTest {
 static String name;
 static int i=0;
`public static String testmethod() {		
	List&lt;String&gt; list= new ArrayList&lt;String&gt;();
	list.add(&quot;Raja&quot;);
	list.add(&quot;Raju&quot;);
	list.add(&quot;Raj&quot;);
	name=list.get(i);
	i++;
	return name;
	
	
}


 public class DependsOnMethodClass extends TestClassTest {

@BeforeMethod()
public void beforecaller(Method m) {	
if(!testmethod().equals(&quot;Raju&quot;)) {
	System.out.println(&quot;This is the method going to be skipped
&quot;+m.getName());
	throw new SkipException(&quot;Data provided is not matching&quot;);


}
else {
	System.out.println(&quot;This is the method not skipped&quot;+m.getName());
}

}

@Test
public void getCall() {
System.out.println(&quot;This is the getCall method&quot;);

}

@Test()
public void getCall1() {
System.out.println(&quot;This is the getCall1 method&quot;);
}
}

Update of TestNG xml: (Important Change)
Adding the parameter configfailurepolicy="continue" will continue the test even we
are throwing skip exception.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot;&gt;
&lt;suite name=&quot;Suite&quot; configfailurepolicy=&quot;continue&quot;&gt;
&lt;test thread-count=&quot;5&quot; name=&quot;Test&quot;&gt;
&lt;classes&gt;
  &lt;class name=&quot;com.testng.newsuite.DependsOnMethodClass&quot;/&gt;
&lt;/classes&gt;
&lt;/test&gt; &lt;!-- Test --&gt;
&lt;/suite&gt; &lt;!-- Suite --&gt;

Output:
This is the method going to be skipped getCall
This is the method not skipped getCall1
This is the getCall1 method

huangapple
  • 本文由 发表于 2020年7月22日 15:42:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63029204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定