简体   繁体   English

TestNG类可以并行运行,但方法不能并行运行。 如何配置testng.xml文件和测试类,以便方法也可以并行运行?

[英]TestNG classes run in parallel, but methods don't. How do I configure my testng.xml file and test classes so methods will also run in parrallel?

I have a suite of Selenium WebDriver tests that I'm running with Grid. 我有一套与Grid一起运行的Selenium WebDriver测试。 When I run this testng.xml file: 当我运行此testng.xml文件时:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" thread-count="20">
  <test name="Test">
    <classes>
      <class name="testcases.test1"/>
      <class name="testcases.test2"/>
      <class name="testcases.test3"/>
      <class name="testcases.test4"/>
      <class name="testcases.test5"/>
      <class name="testcases.test6"/>
      <class name="testcases.test7"/>
      <class name="testcases.test8"/>
      <class name="testcases.test9"/>
      <class name="testcases.test10"/>
      <class name="testcases.test11"/>
      <class name="testcases.test12"/>
      <class name="testcases.test13"/>
      <class name="testcases.test14"/>
      <class name="testcases.test15"/>
      <class name="testcases.test16"/>
      <class name="testcases.test17"/>
      <class name="testcases.test18"/>
      <class name="testcases.test19"/>
      <class name="testcases.test20"/>
      <class name="testcases.test21"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Multiple browsers open, each running the @BeforeClass method and the first @Test method. 打开多个浏览器,每个浏览器都运行@BeforeClass方法和第一个@Test方法。 However, after each browser finishes that first @Test method, it will wait for all the other browsers to finish their respective first @Test method. 但是,每个浏览器完成第一个@Test方法后,它将等待所有其他浏览器完成各自的第一个@Test方法。

The tests are structured like this: 测试的结构如下:

public class sampleTestClass {
    @BeforeClass
    public void setup(){
        //setup Remote WebDriver
    }
    @Test
    public void method1(){
        //do things
    }
    @Test(dependsOnMethods = "method1")
    public void method2(){
        //do things
    }
    @Test(dependsOnMethods = "method2")
    public void method3(){
        //do things
    }
    @AfterClass
    public void teardown(){
        //call teardown
    }

}

with multiple @Test methods in each class. 每个类中有多个@Test方法。 I will add that everything runs the way I prefer when I use this .xml file: 我将补充一点,当我使用此.xml文件时,所有内容都按照我喜欢的方式运行:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MySuite" parallel="tests" thread-count="20">
  <test name="First Test">
    <classes>
       <class name="testcases.test1"/>
    </classes>
  </test>
  <test name="Second Test">
    <classes>
       <class name="testcases.test2"/>
    </classes>
  </test>
  <test name="Third Test">
    <classes>
       <class name="testcases.test3"/>
    </classes>
  </test>
  <test name="Fourth Test">
    <classes>
       <class name="testcases.test4"/>
    </classes>
  </test>
  <!--<test name="Fifth Test">
    <classes>
       <class name="testcases.test5"/>
    </classes>
  </test>-->
  <test name="Sixth Test">
    <classes>
       <class name="testcases.test6"/>
    </classes>
  </test>
  <test name="Seventh Test">
    <classes>
       <class name="testcases.test7"/>
    </classes>
  </test>
</suite>

The reason I'm not okay with this configuration is that one, I don't think it preserves the semantic meaning of the tags at all, and two (and most importantly) I'd like to be able to run the automatically generated suite of failures in parallel, but the configuration of that .xml file is the same as the top one that I'm having trouble with. 我对此配置感到不满意的原因是,一个,我认为它根本没有保留标签的语义,而两个(最重要的是)我希望能够运行自动生成的套件并行失败,但该.xml文件的配置与我遇到问题的前一个相同。

It seems to me that specifying parallel="classes" in the suite tag is consistent with the TestNG documentation, but doesn't achieve asynchronous execution of the @Test methods in the separate classes. 在我看来,在套件标签中指定parallel =“ classes”与TestNG文档一致,但是在单独的类中无法实现@Test方法的异步执行。

Why doesn't this configuration of the .xml file work? 为什么.xml文件的此配置不起作用? What do I need to do differently to make it work? 要使其正常工作,我需要做些什么?

In the context of a webdriver test class having multiple methods, given that there is one instance of a webdriver in that class, i dont see how it would be possible to share one webdriver instance simultanously between methods. 在具有多个方法的webdriver测试类的上下文中,假定该类中有一个webdriver实例,我看不到如何在方法之间同时共享一个webdriver实例。 I seems it would HAVE TO call methods one at a time so that each method had its turn with the webdriver instance. 我似乎必须一次调用一个方法,以便每个方法都可以通过webdriver实例轮流使用。 Otherwise it seems you would get some sort of concurrent exception. 否则,您似乎会得到某种并发异常。

I've tried many times to get parallel methods working with TestNG but never succeeded. 我已经尝试过很多次,以获得与TestNG一起使用的并行方法,但从未成功。 There is an example in the source code though that might be a clue; 源代码中有一个示例,但这可能是一个线索。 just look at the testng.xml in the same folder. 只需查看同一文件夹中的testng.xml。

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

相关问题 testng.xml无法运行类 - testng.xml can't run classes 从testng.xml文件中的其他路径运行测试类 - run test classes from other path in testng.xml file 如何执行包含具有相同@BeforeClass 方法的测试类的 testng.xml 文件 - How to execute testng.xml file which contains test classes having the same @BeforeClass methods 不想在 TESTNG 中并行运行安装程序测试类和 rest 文件 - Don't want to run Setup test classes in parallel and rest File In parallel in TESTNG 当TestNG.xml仅具有类列表而不具有方法列表时,可以通过编程方式调用@Test方法吗? - Can I call @Test method programmatically when TestNG.xml has list of classes only not methods? 如何使用testng并行运行我的硒测试方法 - how to run my selenium test methods in parallel using testng 在并行testng中运行类 - Run classes in parallel testng TestNG:如何以编程方式运行自定义TestNG.XML文件 - TestNG: how do I run a custom TestNG.XML File programmatically TestNG-如何通过testng.xml设置运行@Test方法的顺序? - TestNG - How can I set up the order of running @Test methods through testng.xml? 当我从 testng.xml 运行我的测试套件时,我在 Allure 报告中看到了步骤。 但是当使用“mvn clean test”时我没有看到任何步骤 - When I run my test suite from testng.xml, I see Steps in the Allure report. But when using "mvn clean test" I don't see any steps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM