TestNG conditional configuration methods

后端 未结 2 1789
心在旅途
心在旅途 2021-01-17 04:39

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 co

2条回答
  •  清酒与你
    2021-01-17 05:46

    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.

    Sample code(not tested)

    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
      }
    }
    

    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.

提交回复
热议问题