访问不同类中的测试注释中的值 - TestNg

时间:2016-10-03 19:50:12

标签: java testng

我有一个自定义///<summary> /// Base 64 Encoding with URL and Filename Safe Alphabet using UTF-8 character set. ///</summary> ///<param name="str">The origianl string</param> ///<returns>The Base64 encoded string</returns> public static string Base64ForUrlEncode(string str) { byte[] encbuff = Encoding.UTF8.GetBytes(str); return HttpServerUtility.UrlTokenEncode(encbuff); } ///<summary> /// Decode Base64 encoded string with URL and Filename Safe Alphabet using UTF-8. ///</summary> ///<param name="str">Base64 code</param> ///<returns>The decoded string.</returns> public static string Base64ForUrlDecode(string str) { byte[] decbuff = HttpServerUtility.UrlTokenDecode(str); return Encoding.UTF8.GetString(decbuff); } 我创建如下

Annotation

我的测试扩展了import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface TestConfig { String[] value(); }

BaseClass

现在我需要访问import org.testng.annotations.Test; public class MyTest extends BaseClass { @Test @TestConfig({ "enableCookies" }) public void startTest() { startInstance(); } } @TestConfig注释中的值

import org.testng.annotations.BeforeSuite;

BaseClass

我知道我可以public class BaseClass { public void startInstance() { System.out.println("starting instance"); //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that. } @BeforeSuite public void runChecks() { System.out.println("Checks done...."); } } 但是如何访问TestNG TestConfig config = method.getAnnotation(TestConfig.class)课程?请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以执行类似操作(但删除测试方法中的直接调用):

@BeforeMethod
public void startInstance(Method m) {
    System.out.println("starting instance");
    //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    TestConfig tc = m.getAnnotation(TestConfig.class);
    System.out.println(tc.value());
}