Afa*_*mee 23 java eclipse testng annotations java-5
目前有一种方法可以根据条件禁用TestNG测试
我知道你当前可以在TestNG中禁用测试:
@Test(enabled=false, group={"blah"})
public void testCurrency(){
...
}
Run Code Online (Sandbox Code Playgroud)
我想根据条件禁用相同的测试,但不知道如何.像这样的东西:
@Test(enabled={isUk() ? false : true), group={"blah"})
public void testCurrency(){
...
}
Run Code Online (Sandbox Code Playgroud)
任何人都知道这是否可行.
小智 35
更简单的选择是在检查条件的方法上使用@BeforeMethod注释.如果你想跳过测试,那么只需抛出一个SkipException.像这样:
@BeforeMethod
protected void checkEnvironment() {
if (!resourceAvailable) {
throw new SkipException("Skipping tests because resource was not available.");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道有两种方法可以控制TestNG中的"禁用"测试.
值得注意的区别是,SkipException将突破所有后续测试,而实际上IAnnotationTransformer根据您指定的条件使用Reflection来取消单个测试.我将解释SkipException和IAnnotationTransfomer.
SKIP例外示例
import org.testng.*;
import org.testng.annotations.*;
public class TestSuite
{
// You set this however you like.
boolean myCondition;
// Execute before each test is run
@BeforeMethod
public void before(Method methodName){
// check condition, note once you condition is met the rest of the tests will be skipped as well
if(myCondition)
throw new SkipException();
}
@Test(priority = 1)
public void test1(){}
@Test(priority = 2)
public void test2(){}
@Test(priority = 3)
public void test3(){}
}
Run Code Online (Sandbox Code Playgroud)
IAnnotationTransformer示例
有点复杂,但背后的想法是一个称为反射的概念.
Wiki - http://en.wikipedia.org/wiki/Reflection_(computer_programming)
首先实现IAnnotation接口,将其保存在*.java文件中.
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class Transformer implements IAnnotationTransformer {
// Do not worry about calling this method as testNG calls it behind the scenes before EVERY method (or test).
// It will disable single tests, not the entire suite like SkipException
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){
// If we have chose not to run this test then disable it.
if (disableMe()){
annotation.setEnabled(false);
}
}
// logic YOU control
private boolean disableMe()){
}
Run Code Online (Sandbox Code Playgroud)
然后在你的测试套件java文件中,在@BeforeClass函数中执行以下操作
import org.testng.*;
import org.testng.annotations.*;
/* Execute before the tests run. */
@BeforeClass
public void before(){
TestNG testNG = new TestNG();
testNG.setAnnotationTransformer(new Transformer());
}
@Test(priority = 1)
public void test1(){}
@Test(priority = 2)
public void test2(){}
@Test(priority = 3)
public void test3(){}
Run Code Online (Sandbox Code Playgroud)
最后一步是确保在build.xml文件中添加侦听器.我最终看起来像这样,这只是build.xml中的一行:
<testng classpath="${test.classpath}:${build.dir}" outputdir="${report.dir}"
haltonfailure="false" useDefaultListeners="true"
listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter,Transformer"
classpathref="reportnglibs"></testng>
Run Code Online (Sandbox Code Playgroud)