TestNG conditional configuration methods

后端 未结 2 1788
心在旅途
心在旅途 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:33

    Depending on your test setup you might just use groups:

    public class Test {
    
      @BeforeClass(groups = {"A"})
      public void configuration1() {...}
    
      @BeforeClass(groups = {"B"})
      public void configuration2() {...}
    
      @Test(groups = {"A", "B"})
     public void testFoo() {...} 
    
      @Test(groups = {"A"})
      public void testFoo() {...} 
    }
    

    Or you might think about parameterizing your tests/use data providers.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题