简体   繁体   English

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

[英]Accessing values in annotation of a test in a different class - TestNg

I have a custom Annotation I created as below 我有一个自定义Annotation ,如下所示

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}

I have test that extends a BaseClass . 我进行了扩展BaseClass测试。

import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}

Now I need to access the values inside @TestConfig annotation inside my BaseClass which is below 现在我需要在下面的BaseClass访问@TestConfig批注中的值

import org.testng.annotations.BeforeSuite; 导入org.testng.annotations.BeforeSuite;

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....");
    }
}

I know I can do TestConfig config = method.getAnnotation(TestConfig.class) but how do I access the TestNG TestMethod class? 我知道我可以执行TestConfig config = method.getAnnotation(TestConfig.class)但是如何访问TestNG TestMethod类? Please help. 请帮忙。

You can do something like (but remove the direct call in the test method): 您可以执行类似的操作(但在测试方法中删除直接调用):

@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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM