本文介绍了TestNG条件配置方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以使用不同的配置方法(假设在类级别,@Before/AfterClass)使用户能够选择要在测试用例中使用的配置方法.例如:

I would like to know if there is a way of having different configuration methods (let's say at class level, @Before/AfterClass) that will enable the user to select which configuration method to use in the testcase.For example:

@BeforeClass
public void configuration1 () {
   // Do your configuration1
}

@BeforeClass
public void configuration2 () {
   // Do your configuration2
}

我希望能够选择要使用的配置方法.当然,这可以通过配置方法中的条件来完成,但我想知道是否可以避免在特定运行中运行不包含我想要的配置的方法.

I would like to be able to select which configuration method to use. Of course, this can be done with a condition inside the configuration methods but I would like to know if I can avoid running the method that does not contain the configuration I want in a particular run.

提前致谢!

推荐答案

TestNG 提供 监听器自定义默认功能.根据您的要求,需要实现 IInvokedMethodListener.我不认为命令行参数可以传递给 testng xml,但试试你的运气.在下面的代码中,配置"被假定为来自 testng xml 的参数.

TestNG provides Listeners to customize default functionality. For your requirement, IInvokedMethodListener needs to be implemented. I don't think command line arguments can be passed to a testng xml but try your luck. In below code, "configuration" is assumed to be a parameter from testng xml.

示例代码(未测试)

public class CustomListener implements IInvokedMethodListener {
  @Override
  public void beforeInvocation(IInvokedMethod method, ITestResult itr) {
    if (method.isConfigurationMethod()) {
      String userPassed = method.getTestMethod().getXmlTest()
                .getLocalParameters().get("configuration");
      if(based on userPassed , call configuration() method){

      }
     }
   }

  @Override
  public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {
    // TODO Auto-generated method stub
  }
}

如果您决定从您的 java 类中调用此 Listener,那么您可以读取 cmd 行参数并将其传递给 CustomListener.只是事后的想法.

If you decide to call this Listener from your java class, then you can read the cmd line args and pass it to the CustomListener. Just an after thought.

这篇关于TestNG条件配置方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 23:03