1

我有一个 java maven 项目,我想使用多线程进行测试。我在 src/test 中有 testng.xml,并且配置了 maven surefire 插件以使用它。就像这个页面: http ://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

编辑:添加了surefire pom条目

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.12.4</version>
 <configuration>
  <suiteXmlFiles>
   <suiteXmlFile>src/test/testng.xml</suiteXmlFile>
  </suiteXmlFiles>
 </configuration>
</plugin>

我正在使用万能 2.12.4。

这是我的 testng.xml 文件

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
 <suite name="Testng" parallel="methods" thread-count="3">
  <test name="all" annotations="JDK5">
    <packages>
        <package name="my.package.*"/>
    </packages>
  </test>
</suite>

在几种测试方法中,我有一个打印语句:

System.out.println(Thread.currentThread().getName());

每次运行(pool-1-thread-1)总是具有相同的输出。如果 testng 并行运行,那么将会有不同的线程在运行。

我的问题是:为什么 testng 没有运行多线程?有没有更好的方法来检查 testng 是否正在运行多线程?

编辑:类或方法

当我使用线程运行时,每个测试类似乎都在它自己的线程中运行,但不是每个方法。在 testng.xml 我设置了 parallel="methods" 所以它应该按照方法来做。它不能按方法做吗?

4

2 回答 2

0

我找到了答案,我没有给它足够的线程。由于我的项目有很多测试 testng 不会为每个方法创建新线程。我将其更改为 10 个线程并修复了它。

于 2013-02-19T16:24:08.233 回答
0

在这种情况下,TestNG 不会并行运行。您需要定义 ThreadLocal 以在线程中定义不同的浏览器。使用:ThreadLocal,并在不同的实例中定义浏览器。在这种情况下,您将获得不同的浏览器 ID。

于 2014-02-22T04:04:25.520 回答