简体   繁体   English

testng 是否运行多线程?

[英]Is testng running multithreaded?

I have a java maven project that I want tested using multithreads.我有一个想要使用多线程进行测试的 java maven 项目。 I have the testng.xml in src/test and the maven surefire plugin is configured to used it.我在 src/test 中有 testng.xml 并且 maven surefire 插件被配置为使用它。 Just like this page: http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html就像这个页面: http : //maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

edit: added surefire pom entry编辑:添加了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>

I am using surefire 2.12.4.我正在使用surefire 2.12.4。

This is my testng.xml file这是我的 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>

Inside several test methods I have a print statement:在几个测试方法中,我有一个打印语句:

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

That always has the same output every run (pool-1-thread-1).每次运行总是具有相同的输出(pool-1-thread-1)。 If testng was running parallel then there would be different threads running.如果 testng 是并行运行的,那么就会有不同的线程在运行。

My questions are: why is testng not running multithreaded?我的问题是:为什么 testng 没有运行多线程? and is there a better way to check if testng is running multithreaded?有没有更好的方法来检查 testng 是否正在运行多线程?

edit: classes or methods编辑:类或方法

When I run with threads each test class seems to run in it's own thread, but not each method.当我使用线程运行时,每个测试类似乎都在它自己的线程中运行,但不是每个方法。 In the testng.xml I set parallel="methods" so it should do it per method.在 testng.xml 中,我设置了 parallel="methods" 所以它应该按照方法来做。 Can it not do per method?不能按方法做吗?

I found the answer, I wasn't giving it enough threads.我找到了答案,我没有给它足够的线程。 Since my project has many tests testng would not make new threads per method.由于我的项目有很多测试 testng 不会为每个方法创建新线程。 I changed it to 10 threads and that fixed it.我将其更改为 10 个线程并修复了它。

TestNG will not be running in parallel in this case.在这种情况下,TestNG 将不会并行运行。 You need to define ThreadLocal to define different browsers in threads.您需要定义 ThreadLocal 以在线程中定义不同的浏览器。 Use: ThreadLocal, and define the browsers in different instances.使用:ThreadLocal,定义不同实例的浏览器。 You will be getting different browser ID's in this case..在这种情况下,您将获得不同的浏览器 ID。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM