2017-08-28 78 views
1

基本上,我想使用Context类的常量布尔属性,通过反射更改了此属性,以便我可以动态设置@annotated为testNG方法启用一个TestNG类。 TestNG类具有与Context.DISBLE_TEST_CASES_IF_OLD_STACK相同的静态final属性。我已经粘贴了TestNG类及其方法的代码。 对我来说,最终目标是切换启用值或基本上禁用基于该背景下,如果旧环境或新环境注解属性Test.enabled的值必须是一个常量表达式

package com.abc.api.core.context; 

    import java.lang.reflect.Field; 
    import java.lang.reflect.Modifier; 


    public class Context { 
     public static final boolean DISBLE_TEST_CASES_IF_OLD_STACK = getConstantReflection(); 


     public static boolean getConstantReflection() 
     { 
      System.out.println(DISBLE_TEST_CASES_IF_OLD_STACK); 
      try { 
       setEnableFlagBasedOnStackForTestCases(); 
      } catch (NoSuchFieldException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SecurityException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalAccessException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      System.out.println(Context.DISBLE_TEST_CASES_IF_OLD_STACK); 
      try { 
       final Field fld = Context.class.getDeclaredField("DISBLE_TEST_CASES_IF_OLD_STACK"); 
       return (Boolean) fld.get(null); 
      } catch (NoSuchFieldException e) { 
       return (Boolean) null; 
      } catch (IllegalAccessException e) { 
       return (Boolean) null; 
      } 
     } 

     private static void setEnableFlagBasedOnStackForTestCases() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ 


      Field f = Context.class.getDeclaredField("DISBLE_TEST_CASES_IF_OLD_STACK"); 
      f.setAccessible(true); 

      //'modifiers' - it is a field of a class called 'Field'. Make it accessible and remove 
      //'final' modifier for our 'CONSTANT' field 
      Field modifiersField = Field.class.getDeclaredField("modifiers"); 
      modifiersField.setAccessible(true); 
      modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL); 

      if (TestcaseContext.getContext().equalsIgnoreCase(Context.OLD_STACK)) { 
       f.setBoolean(null, false); 
      }else { 
       f.setBoolean(null, true); 
      } 
     } 

    } 

TestNG的类和方法的例子测试:

package com.abc.api.test.tests.TestA; 

import com.abc.api.core.context.Context; 

public class TestA extends TestCommon { 

    private static final boolean ENABLE_DISABLE = Context.DISBLE_TEST_CASES_IF_OLD_STACK; 

    /** 
    * 
    */ 
    @BeforeTest 
    void setPropertiesFile() { 
     ...... 

    } 

    /** 
    * PATCH Positive Test Cases 
    * 
    */ 

    @Test(priority = 11, enabled=ENABLE_DISABLE) 
    public void testPositive1() { 
     ...... 
    } 
} 
+2

您的'DISBLE_TEST_CASES_IF_OLD_STACK'不是一个常量变量。你将无法在注释中使用它。 –

+0

但并不是反射本质上修改静态最终值,它实际上是类的常量属性。因此我经历了使用反射的这条路。 –

+2

https://stackoverflow.com/questions/10636201/java-annotations-values-provided-in-dynamic-manner –

回答

2

为了有选择地禁用测试用例的最终目标,可以使用TestNgs IAnnotationTransformer来控制启用标志。它将在每个@Test注释方法之前运行,并且可以控制它的执行。

例如。

public class DemoTransformer implements IAnnotationTransformer { 
    public void transform(ITest annotation, Class testClass, 
     Constructor testConstructor, Method testMethod) 
    { 
    if (condition) { 
     annotation.setEnabled(false/true); 
    } 
} 
+0

这正是我最终做的。然而,由于某些原因,当我用listener类注释TestNG类时,侦听器未被调用。我不得不在测试套件中通过xml使用监听器,这很糟糕,因为我想将这个监听器应用于这个特定的类。另外,我发现我可以使用组并排除,并根据需要包含它们。 –

相关问题